1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/dom", |
---|
4 | "./_Container" |
---|
5 | ], function(declare, dom, _Container){ |
---|
6 | /*===== |
---|
7 | declare = dojo.declare; |
---|
8 | dom = dojo.dom; |
---|
9 | _Container = dojox.mvc._Container; |
---|
10 | =====*/ |
---|
11 | |
---|
12 | return declare("dojox.mvc.Repeat", [_Container], { |
---|
13 | // summary: |
---|
14 | // A model-bound container which binds to a collection within a data model |
---|
15 | // and produces a repeating user-interface from a template for each |
---|
16 | // iteration within the collection. |
---|
17 | // |
---|
18 | // description: |
---|
19 | // A repeat is bound to an intermediate dojo.Stateful node corresponding |
---|
20 | // to an array in the data model. Child dijits or custom view components |
---|
21 | // inside it inherit their parent data binding context from it. |
---|
22 | |
---|
23 | // index: Integer |
---|
24 | // An index used to track the current iteration when the repeating UI is |
---|
25 | // produced. This may be used to parameterize the content in the repeat |
---|
26 | // template for the current iteration. |
---|
27 | // |
---|
28 | // For example, consider a collection of search or query results where |
---|
29 | // each item contains a "Name" property used to prime the "Results" data |
---|
30 | // model. Then, the following CRUD-style UI displays all the names in |
---|
31 | // the search results in text boxes where they may be updated or such. |
---|
32 | // |
---|
33 | // | <div dojoType="dojox.mvc.Repeat" ref="Results"> |
---|
34 | // | <div class="row" dojoType="dojox.mvc.Group" ref="${this.index}"> |
---|
35 | // | <label for="nameInput${this.index}">Name:</label> |
---|
36 | // | <input dojoType="dijit.form.TextBox" id="nameInput${this.index}" ref="'Name'"></input> |
---|
37 | // | </div> |
---|
38 | // | </div> |
---|
39 | index : 0, |
---|
40 | |
---|
41 | // summary: |
---|
42 | // Override and save template from body. |
---|
43 | postscript: function(params, srcNodeRef){ |
---|
44 | this.srcNodeRef = dom.byId(srcNodeRef); |
---|
45 | if(this.srcNodeRef){ |
---|
46 | if(this.templateString == ""){ // only overwrite templateString if it has not been set |
---|
47 | this.templateString = this.srcNodeRef.innerHTML; |
---|
48 | } |
---|
49 | this.srcNodeRef.innerHTML = ""; |
---|
50 | } |
---|
51 | this.inherited(arguments); |
---|
52 | }, |
---|
53 | |
---|
54 | ////////////////////// PRIVATE METHODS //////////////////////// |
---|
55 | |
---|
56 | _updateBinding: function(name, old, current){ |
---|
57 | // summary: |
---|
58 | // Rebuild repeating UI if data binding changes. |
---|
59 | // tags: |
---|
60 | // private |
---|
61 | this.inherited(arguments); |
---|
62 | this._buildContained(); |
---|
63 | }, |
---|
64 | |
---|
65 | _buildContained: function(){ |
---|
66 | // summary: |
---|
67 | // Destroy any existing contained view, recreate the repeating UI |
---|
68 | // markup and parse the new contents. |
---|
69 | // tags: |
---|
70 | // private |
---|
71 | |
---|
72 | // TODO: Potential optimization: only create new widgets for insert, only destroy for delete. |
---|
73 | this._destroyBody(); |
---|
74 | this._updateAddRemoveWatch(); |
---|
75 | |
---|
76 | var insert = ""; |
---|
77 | for(this.index = 0; this.get("binding").get(this.index); this.index++){ |
---|
78 | insert += this._exprRepl(this.templateString); |
---|
79 | } |
---|
80 | var repeatNode = this.srcNodeRef || this.domNode; |
---|
81 | repeatNode.innerHTML = insert; |
---|
82 | |
---|
83 | // srcNodeRef is used in _createBody, so in the programmatic create case where repeatNode was set |
---|
84 | // from this.domNode we need to set srcNodeRef from repeatNode |
---|
85 | this.srcNodeRef = repeatNode; |
---|
86 | |
---|
87 | this._createBody(); |
---|
88 | }, |
---|
89 | |
---|
90 | _updateAddRemoveWatch: function(){ |
---|
91 | // summary: |
---|
92 | // Updates the watch handle when binding changes. |
---|
93 | // tags: |
---|
94 | // private |
---|
95 | if(this._addRemoveWatch){ |
---|
96 | this._addRemoveWatch.unwatch(); |
---|
97 | } |
---|
98 | var pThis = this; |
---|
99 | this._addRemoveWatch = this.get("binding").watch(function(name,old,current){ |
---|
100 | if(/^[0-9]+$/.test(name.toString())){ |
---|
101 | if(!old || !current){ |
---|
102 | pThis._buildContained(); |
---|
103 | } // else not an insert or delete, will get updated in above |
---|
104 | } |
---|
105 | }); |
---|
106 | } |
---|
107 | }); |
---|
108 | }); |
---|