Now.js Framework Documentation
TemplateManager
TemplateManager
หน้านี้สร้างจากซอร์สโดยตรงเมื่อ 2026-08-21 คำอธิบายมาจาก JSDoc ในไฟล์
รันซ้ำได้ด้วยdocs_scaffoldเมื่อโค้ดเปลี่ยน อย่าแก้ด้วยมือถ้าตั้งใจให้ generate ต่อ
ที่มา
- ไฟล์:
Now/js/TemplateManager.js(3985 บรรทัด) - รูปแบบ: object literal (singleton)
เมธอดสาธารณะ (68)
applyModelBindingDefinition(binding, value)
ยังไม่มีคำอธิบายในซอร์ส
cleanup(container?)
ล้าง template cache, handler ที่ลงทะเบียนไว้ และ binding ภายใต้ container (ค่าเริ่มต้น: element หลัก Now.config.mainSelector) RouterManager ส่ง outlet ของ layout มา layout ที่ครอบหน้าอยู่จึงยังเก็บ binding ของตัวเองไว้ข้ามการเปลี่ยนหน้า
cleanupCache()
ยังไม่มีคำอธิบายในซอร์ส
cleanupComponent(componentId)
ยังไม่มีคำอธิบายในซอร์ส
cleanupComponentHandlers(componentId)
ยังไม่มีคำอธิบายในซอร์ส
cleanupElement(element)
ยังไม่มีคำอธิบายในซอร์ส
cleanupElementBindings(element, options = {})
ยังไม่มีคำอธิบายในซอร์ส
cleanupElementScripts(element)
ยังไม่มีคำอธิบายในซอร์ส
cleanupHandlers()
ยังไม่มีคำอธิบายในซอร์ส
cleanupModelListeners(element)
ยังไม่มีคำอธิบายในซอร์ส
cleanupScripts()
Cleanup all active scripts before page navigation Calls cleanup functions if provided by the script
clearCache()
ยังไม่มีคำอธิบายในซอร์ส
cloneDirectiveState(state)
เก็บสำเนาชั้นบนสุดของ state ตอนสร้าง binding ของ directive ค่าที่ซ้อนอยู่ข้างในใช้ร่วมกัน ไม่คัดลอก — สำเนานี้มีไว้ให้คีย์ที่ state ปัจจุบันไม่มีแล้วเท่านั้น เพราะ getDirectiveEvalState() เอา state ปัจจุบันทับลงบนสำเนาเสมอ
getDirectiveContext(binding, context)
ยังไม่มีคำอธิบายในซอร์ส
getDirectiveEvalState(binding, context, options = {})
ยังไม่มีคำอธิบายในซอร์ส
hasOperators(str)
ยังไม่มีคำอธิบายในซอร์ส
async init(options = {})
ยังไม่มีคำอธิบายในซอร์ส
isValidPath(path)
ยังไม่มีคำอธิบายในซอร์ส
isValidUrl(url)
ยังไม่มีคำอธิบายในซอร์ส
async loadFromServer(path)
ยังไม่มีคำอธิบายในซอร์ส
mergeDirectiveContext(previousContext, context)
ยังไม่มีคำอธิบายในซอร์ส
mergeDirectiveState(previousState, nextState)
ยังไม่มีคำอธิบายในซอร์ส
normalizePath(path)
ยังไม่มีคำอธิบายในซอร์ส
onComponentDestroy(component)
ยังไม่มีคำอธิบายในซอร์ส
performCleanup()
ยังไม่มีคำอธิบายในซอร์ส
async processContent(content, path)
แปลง markup ของ template ที่โหลดมาเป็น subtree ที่ประมวลแล้ว — parse ใน <template> (เฉื่อย: ไม่มีอะไรในนั้นโหลดหรือทำงาน) ทำความสะอาดระดับ trusted ด้วย sanitizeElement() แล้วประมวล directive และ interpolation ของ component ที่ลงทะเบียนกับ path จากนั้นทำความสะอาดผลลัพธ์อีกรอบ
processDataAttr(el, value, context)
ทำงานของ data-attr แยก binding ด้วย comma ระดับบนสุด แต่ละค่าผ่าน safeAttributeValue() ก่อนเขียน URL จากข้อมูลจึงเป็น javascript: ไม่ได้ และ on*, srcdoc, directive ที่ระบุโค้ดไม่ถูกตั้งเลย ดู data-attr
processDataBind(el, expression, context)
Process data-bind directive (or data-attr="data:fieldName") for binding complex data to TableManager or LineItemsManager components. Uses ExpressionEvaluator so it supports bracket access and {{}} interpolation — works correctly inside data-for loops. Includes a retry mechanism to wait for the component to be initialized (TableManager / LineItemsManager may initialize asynchronously). Usage: data-bind="items" → state.items data-attr="data:items" → same (backward compatible) data-bind="order.lines" → state.order.lines
processDataChecked(el, expression, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataClass(el, value, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataContainer(el, value, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataDirectives(element, context, options = {})
Apply every data-* directive found in a subtree nested data-component="api" alone · that component renders its own subtree from its own (merged) scope, and painting it here with the outer scope answers its conditions from data that does not describe it — a data-if reading a field the outer response has never heard of resolves to undefined and removes the element for good
processDataFiles(el, expression, context)
Process data-files directive for file inputs. Resolves an expression to obtain existing file data and sets it on the file input element. Uses ExpressionEvaluator so all expression syntax (dot notation, bracket access, mustache interpolation) is supported — exactly the same as data-attr or data-text. Usage: data-files="avatar" → state.avatar data-files="config.csv" → state.config.csv data-files="default_icon[{{lng.value}}]" → state.default_icon['th'] The resolved value must be an array of file objects [{url, name}].
processDataFor(el, value, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataHtml(el, value, context)
ทำงานของ data-html ค่าที่ได้เป็นข้อมูล จึงผ่าน SecurityManager.sanitizeHtml() ระดับ untrusted ทุกครั้งก่อนใส่ ไม่ขึ้นกับ security.sanitize — ดู data-html
processDataIf(el, expression, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataModel(el, value, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataOn(el, value, context)
ทำงานของ data-on แยก binding และอาร์กิวเมนต์ด้วย splitTopLevel() (string ในเครื่องหมายคำพูดเก็บ comma และวงเล็บไว้) handler คือ method ของ component หรือสิ่งที่ Now.resolveFunction() คืน และทำงานทุก event — ไม่มี throttle ในตัว ดู data-on
processDataOnLoad(element, context)
ทำงานของ data-on-load: เรียกฟังก์ชันที่ Now.resolveFunction() หาได้จากค่าของ attribute เป็น fn(element, context) และเก็บ cleanup ที่คืนมา ดู data-on-load
processDataOptionsKey(el, expression, context)
Process data-options-key directive for select, multi-select, tags, and text-autocomplete elements. Detects the element type and delegates to the appropriate Factory's populateFromOptions method. The options data is retrieved from context.options (set by FormManager from instance.state.formOptions). Resolves {{}} against context.state/computed (for data-for loop vars), then looks up option arrays by flat key, dot path, or bracket path on options only (e.g. attributeoptions[{{attr.id}}]). Usage: data-options-key="categories" data-options-key="attr{{attr.id}}_options" data-options-key="attribute_options[{{attr.id}}]"
processDataScript(element, context)
ทำงานของ data-script: เรียกฟังก์ชันที่ Now.resolveFunction() หาได้จากค่าของ attribute เป็น fn(element, data) หลังประมวล template และเก็บ cleanup ที่คืนมา ดู data-script
processDataStyle(el, value, context)
ยังไม่มีคำอธิบายในซอร์ส
processDataText(el, expression, context)
ยังไม่มีคำอธิบายในซอร์ส
processInterpolation(element, context)
แทน {{ expr }} ใน text node และใน attribute ที่ไม่ใช่ data-* (รวม data-attr/data-options-key) ค่าถูกใส่ตามที่เป็นในรอบเดียว ($& ยังเป็น $& · ค่าที่มี {{ }} ไม่ถูกประเมินซ้ำ) ค่าใน attribute ผ่าน safeAttributeValue() · เนื้อหาของ element ที่มี data-html ถูกข้ามเพราะเป็นข้อมูล · node ที่ binding เขียนแล้ว (markBound: element ของ data-text, รายการของ data-for, text node ที่ {{ }} เติมแล้ว) และ attribute ที่ binding เขียนแล้ว (data-attr, {{ }} ใน attribute) ถูกข้ามเช่นกัน — {{ }} ที่มาจากค่าจึงไม่ถูกประเมิน
markBound(node) / isBound(node)
ทำเครื่องหมาย node ที่เนื้อหามาจาก binding (element = ทั้ง subtree) / ตรวจเครื่องหมาย · processInterpolation() ไม่เดินเข้า node ที่มีเครื่องหมาย
processTemplate(element, context)
ประมวล directive และ {{ }} ของ subtree ที่อยู่ในหน้าแล้วด้วย context แล้ว scan หา element/form ถ้าเปิด security.sanitize จะทำความสะอาด subtree ระดับ trusted ก่อน (sanitizeElement()) — ยกเว้นเมื่อ context เป็น component ที่ mount แล้วกำลังวาดซ้ำ (context._mounted) เพราะ markup นั้นถูกทำความสะอาดตอนใส่เข้ามาแล้ว
processTemplateContent(element, context)
ยังไม่มีคำอธิบายในซอร์ส
processTemplateString(template, context, container)
ยังไม่มีคำอธิบายในซอร์ส
recordError(error, path)
ยังไม่มีคำอธิบายในซอร์ส
recordPerformance(name, data)
ยังไม่มีคำอธิบายในซอร์ส
registerEventHandler(handlerExpr, context)
ยังไม่มีคำอธิบายในซอร์ส
render(context, container)
ยังไม่มีคำอธิบายในซอร์ส
renderError(error)
markup ของกล่องข้อผิดพลาดที่ใส่แทน template ที่ประมวลผลไม่ได้ · error.message ถูก escape (SecurityManager.escapeHtml) จึงแสดงเป็นข้อความเสมอ
cleanUrlAttributes(root)
ตรวจ attribute ที่เป็น URL (SecurityManager.isUrlAttribute() และ srcset) ของ root และ element ข้างในด้วย safeAttributeValue(): ค่าที่ถูกปฏิเสธถูกลบ ค่าที่ถูกแก้ถูกเขียนแทน · ใช้กับ markup ที่ค่า attribute ถูกเติมจากข้อมูลหลังทำความสะอาดแล้ว (เซลล์ data-template ของ TableManager)
safeAttributeValue(el, name, value)
ค่าที่ directive (data-attr, {{ }} ใน attribute) เขียนได้ หรือ null เมื่อห้ามเขียน: ชื่อที่ SecurityManager.isForbiddenAttribute() ห้าม (on*, srcdoc, http-equiv, data-on, data-action, data-script, data-on-load, data-html, data-component, data-api-*, data-attr, data-if, data-for, data-model, data-bind) ห้ามเสมอ — กฎชุดเดียวกับ ResponseHandler · attribute URL ตาม SecurityManager.isUrlAttribute() (href, src, action, formaction, poster, cite, background, ping, xlink:href, data ฯลฯ) เฉพาะ URL ที่ SecurityManager.sanitizeUrl() รับ (src/poster รับ data:image ด้วย) · srcset ตรวจทุกตัว · style ที่มีสคริปต์ถูกตัด
sanitizeElement(element)
ทำความสะอาด template ระดับ trusted ของ SecurityManager (SecurityManager.sanitizeTree(element, {trusted: true}) หรือ sanitizeHtml(..., {trusted: true}) เมื่อส่ง string มา)
- แท็กที่ไม่อยู่ใน
SecurityManager.config.html.templateTagsถูกลบพร้อมเนื้อหา (<script>,<base>ฯลฯ) - attribute ที่ถูกลบ:
on*ทุกตัว,srcdoc,http-equiv, attribute URL (href,src,action,formaction,posterฯลฯ) ที่ scheme ไม่ใช่http:,https:,mailto:,tel:(data:image/...คงไว้) และstyleที่มีexpression(,javascript:หรือbehavior: - ที่เหลือคงตามที่ผู้เขียน template เขียน:
style(รวม custom property),<style>,<link>, ฟอร์ม, URL แบบ../และ?t=12:30, directivedata-*
พฤติกรรมให้เขียนด้วย JavaScript (data-on, data-action, data-script) ไม่ใช่ attribute on* เพราะ template จะถูกตัดทิ้ง
sanitizeStyles(styles)
ยังไม่มีคำอธิบายในซอร์ส
sanitizeUrlAttribute(value)
คืน SecurityManager.sanitizeUrl(value) หรือ null เมื่อ URL ถูกปฏิเสธ
sanitizeUrlValue(value)
Sanitize URL values in CSS to prevent XSS attacks Supports: http://, https://, relative paths, and data:image/* (including base64) Blocks: javascript:, data:text/html, and other dangerous protocols
scheduleI18nUpdate(container)
ยังไม่มีคำอธิบายในซอร์ส
setStateValue(path, value, state)
ยังไม่มีคำอธิบายในซอร์ส
setupPerformanceMonitoring()
ยังไม่มีคำอธิบายในซอร์ส
splitTopLevel(value, separator = ',')
แยกค่าของ directive ด้วย separator เฉพาะที่อยู่นอกเครื่องหมายคำพูด วงเล็บ ก้ามปู และปีกกา: "a('x, y'), b(1, 2)" → ["a('x, y')", "b(1, 2)"] backslash ใน string ทำให้ตัวถัดไปเป็นตัวอักษรธรรมดา คืนส่วนที่ตัดช่องว่างแล้วและไม่ว่าง
startCleanupInterval()
เริ่มตัวจับเวลาล้างข้อมูลเป็นระยะ หยุดเมื่อปิดหน้าหรือหน้าถูกซ่อน และเริ่มใหม่เมื่อหน้ากลับมาแสดง ผูก listener ของหน้าเพียงครั้งเดียวไม่ว่าจะเรียกกี่ครั้ง
stopCleanupInterval()
หยุดตัวจับเวลาล้างข้อมูล
syncDirectiveBinding(binding, context, options = {})
ยังไม่มีคำอธิบายในซอร์ส
async validateMarkup(content)
ยังไม่มีคำอธิบายในซอร์ส
async validateRequest(url)
ยังไม่มีคำอธิบายในซอร์ส
valueToString(value)
ยังไม่มีคำอธิบายในซอร์ส
เหตุการณ์
| Event | เมื่อเกิด | Detail |
|---|---|---|
template:render |
ประมวลผล template ของ component เสร็จ | {componentId, timestamp, container} |
template:error |
โหลดหรือประมวลผล template ไม่สำเร็จ | {error, path, timestamp} |
template:performance |
รายงานเวลาที่ใช้ประมวลผล | {name, timestamp} |
template:cleanup |
คืนทรัพยากรของ component หนึ่งตัว | {componentId} |
template:cleanup:all |
คืนทรัพยากรทั้งหมด | ไม่มี |
if:updated |
data-if ประเมินผลใหม่ |
{element, condition, expression} |
app:cleanup:start |
เริ่มรอบเก็บกวาดตามเวลา | {timestamp} |
app:cleanup:end |
จบรอบเก็บกวาด | {timestamp} |
Data attribute ที่ใช้
data-attr · data-component-id · data-element · data-files · data-format · data-formatter · data-formatters · data-handler-id · data-on-load · data-options-key · data-parent-context · data-script · data-table
พึ่งพาคลาสอื่น
ApiComponent · ComponentManager · CoreObserver · ElementManager · ErrorManager · EventManager · EventSystemManager · ExpressionEvaluator · FileElementFactory · I18nManager · LineItemsManager · MultiSelectElementFactory · SelectElementFactory · TableManager · TagsElementFactory · TextElementFactory · Utils