1 | define([ |
---|
2 | "dojo/_base/array", // forEach() |
---|
3 | "dojo/aspect", // after() |
---|
4 | "dojo/_base/declare", // declare() |
---|
5 | "dojo/_base/lang", // hitch() |
---|
6 | "dojo/parser" // parse() |
---|
7 | ], function(array, aspect, declare, lang, parser){ |
---|
8 | |
---|
9 | // module: |
---|
10 | // dijit/_WidgetsInTemplateMixin |
---|
11 | |
---|
12 | return declare("dijit._WidgetsInTemplateMixin", null, { |
---|
13 | // summary: |
---|
14 | // Mixin to supplement _TemplatedMixin when template contains widgets |
---|
15 | |
---|
16 | // _earlyTemplatedStartup: Boolean |
---|
17 | // A fallback to preserve the 1.0 - 1.3 behavior of children in |
---|
18 | // templates having their startup called before the parent widget |
---|
19 | // fires postCreate. Defaults to 'false', causing child widgets to |
---|
20 | // have their .startup() called immediately before a parent widget |
---|
21 | // .startup(), but always after the parent .postCreate(). Set to |
---|
22 | // 'true' to re-enable to previous, arguably broken, behavior. |
---|
23 | _earlyTemplatedStartup: false, |
---|
24 | |
---|
25 | // widgetsInTemplate: [protected] Boolean |
---|
26 | // Should we parse the template to find widgets that might be |
---|
27 | // declared in markup inside it? (Remove for 2.0 and assume true) |
---|
28 | widgetsInTemplate: true, |
---|
29 | |
---|
30 | // contextRequire: Function |
---|
31 | // Used to provide a context require to the dojo/parser in order to be |
---|
32 | // able to use relative MIDs (e.g. `./Widget`) in the widget's template. |
---|
33 | contextRequire: null, |
---|
34 | |
---|
35 | _beforeFillContent: function(){ |
---|
36 | if(this.widgetsInTemplate){ |
---|
37 | // Before copying over content, instantiate widgets in template |
---|
38 | var node = this.domNode; |
---|
39 | |
---|
40 | if(this.containerNode && !this.searchContainerNode){ |
---|
41 | // Tell parse call below not to look for widgets inside of this.containerNode |
---|
42 | this.containerNode.stopParser = true; |
---|
43 | } |
---|
44 | |
---|
45 | parser.parse(node, { |
---|
46 | noStart: !this._earlyTemplatedStartup, |
---|
47 | template: true, |
---|
48 | inherited: {dir: this.dir, lang: this.lang, textDir: this.textDir}, |
---|
49 | propsThis: this, // so data-dojo-props of widgets in the template can reference "this" to refer to me |
---|
50 | contextRequire: this.contextRequire, |
---|
51 | scope: "dojo" // even in multi-version mode templates use dojoType/data-dojo-type |
---|
52 | }).then(lang.hitch(this, function(widgets){ |
---|
53 | this._startupWidgets = widgets; |
---|
54 | |
---|
55 | // _WidgetBase::destroy() will destroy any supporting widgets under this.domNode. |
---|
56 | // If we wanted to, we could call this.own() on anything in this._startupWidgets that was moved outside |
---|
57 | // of this.domNode (like Dialog, which is moved to <body>). |
---|
58 | |
---|
59 | // Hook up attach points and events for nodes that were converted to widgets |
---|
60 | for(var i = 0; i < widgets.length; i++){ |
---|
61 | this._processTemplateNode(widgets[i], function(n,p){ |
---|
62 | // callback to get a property of a widget |
---|
63 | return n[p]; |
---|
64 | }, function(widget, type, callback){ |
---|
65 | // callback to do data-dojo-attach-event to a widget |
---|
66 | if(type in widget){ |
---|
67 | // back-compat, remove for 2.0 |
---|
68 | return widget.connect(widget, type, callback); |
---|
69 | }else{ |
---|
70 | // 1.x may never hit this branch, but it's the default for 2.0 |
---|
71 | return widget.on(type, callback, true); |
---|
72 | } |
---|
73 | }); |
---|
74 | } |
---|
75 | |
---|
76 | // Cleanup flag set above, just in case |
---|
77 | if(this.containerNode && this.containerNode.stopParser){ |
---|
78 | delete this.containerNode.stopParser; |
---|
79 | } |
---|
80 | })); |
---|
81 | |
---|
82 | if(!this._startupWidgets){ |
---|
83 | throw new Error(this.declaredClass + ": parser returned unfilled promise (probably waiting for module auto-load), " + |
---|
84 | "unsupported by _WidgetsInTemplateMixin. Must pre-load all supporting widgets before instantiation."); |
---|
85 | } |
---|
86 | } |
---|
87 | }, |
---|
88 | |
---|
89 | _processTemplateNode: function(/*DOMNode|Widget*/ baseNode, getAttrFunc, attachFunc){ |
---|
90 | // Override _AttachMixin._processNode to skip DOMNodes with data-dojo-type set. They are handled separately |
---|
91 | // in the _beforeFillContent() code above. |
---|
92 | |
---|
93 | if(getAttrFunc(baseNode, "dojoType") || getAttrFunc(baseNode, "data-dojo-type")){ |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | return this.inherited(arguments); |
---|
98 | }, |
---|
99 | |
---|
100 | startup: function(){ |
---|
101 | array.forEach(this._startupWidgets, function(w){ |
---|
102 | if(w && !w._started && w.startup){ |
---|
103 | w.startup(); |
---|
104 | } |
---|
105 | }); |
---|
106 | this._startupWidgets = null; |
---|
107 | this.inherited(arguments); |
---|
108 | } |
---|
109 | }); |
---|
110 | }); |
---|