1 | define([ |
---|
2 | "dojo/_base/kernel", |
---|
3 | "dojo/_base/lang", |
---|
4 | "../_base", |
---|
5 | "dojo/_base/array" |
---|
6 | ], function(kernel,lang,dd,array){ |
---|
7 | /*===== |
---|
8 | dd = dojox.dtl; |
---|
9 | =====*/ |
---|
10 | lang.getObject("dojox.dtl.contrib.data", true); |
---|
11 | |
---|
12 | var ddcd = dd.contrib.data; |
---|
13 | var first = true; |
---|
14 | |
---|
15 | ddcd._BoundItem = lang.extend(function(item, store){ |
---|
16 | this.item = item; |
---|
17 | this.store = store; |
---|
18 | }, |
---|
19 | { |
---|
20 | get: function(key){ |
---|
21 | var store = this.store; |
---|
22 | var item = this.item; |
---|
23 | |
---|
24 | if(key == "getLabel"){ |
---|
25 | return store.getLabel(item); |
---|
26 | }else if(key == "getAttributes"){ |
---|
27 | return store.getAttributes(item); |
---|
28 | }else if(key == "getIdentity"){ |
---|
29 | if(store.getIdentity){ |
---|
30 | return store.getIdentity(item); |
---|
31 | } |
---|
32 | return "Store has no identity API"; |
---|
33 | }else{ |
---|
34 | if(!store.hasAttribute(item, key)){ |
---|
35 | if(key.slice(-1) == "s"){ |
---|
36 | if(first){ |
---|
37 | first = false; |
---|
38 | kernel.deprecated("You no longer need an extra s to call getValues, it can be figured out automatically"); |
---|
39 | } |
---|
40 | key = key.slice(0, -1); |
---|
41 | } |
---|
42 | if(!store.hasAttribute(item, key)){ |
---|
43 | return; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | var values = store.getValues(item, key); |
---|
48 | if(!values){ |
---|
49 | return; |
---|
50 | } |
---|
51 | if(!lang.isArray(values)){ |
---|
52 | return new ddcd._BoundItem(values, store); |
---|
53 | } |
---|
54 | |
---|
55 | values = array.map(values, function(value){ |
---|
56 | if(lang.isObject(value) && store.isItem(value)){ |
---|
57 | return new ddcd._BoundItem(value, store); |
---|
58 | } |
---|
59 | return value; |
---|
60 | }); |
---|
61 | values.get = ddcd._get; |
---|
62 | return values; |
---|
63 | } |
---|
64 | } |
---|
65 | }); |
---|
66 | ddcd._BoundItem.prototype.get.safe = true; |
---|
67 | |
---|
68 | ddcd.BindDataNode = lang.extend(function(items, query, store, alias){ |
---|
69 | this.items = items && new dd._Filter(items); |
---|
70 | this.query = query && new dd._Filter(query); |
---|
71 | this.store = new dd._Filter(store); |
---|
72 | this.alias = alias; |
---|
73 | }, |
---|
74 | { |
---|
75 | render: function(context, buffer){ |
---|
76 | var items = this.items && this.items.resolve(context); |
---|
77 | var query = this.query && this.query.resolve(context); |
---|
78 | var store = this.store.resolve(context); |
---|
79 | if(!store || !store.getFeatures){ |
---|
80 | throw new Error("data_bind didn't receive a store"); |
---|
81 | } |
---|
82 | |
---|
83 | if(query){ |
---|
84 | var sync = false; |
---|
85 | |
---|
86 | store.fetch({ |
---|
87 | query: query, |
---|
88 | sync: true, |
---|
89 | scope: this, |
---|
90 | onComplete: function(it){ |
---|
91 | sync = true; |
---|
92 | items = it; |
---|
93 | } |
---|
94 | }); |
---|
95 | |
---|
96 | if(!sync){ |
---|
97 | throw new Error("The bind_data tag only works with a query if the store executed synchronously"); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | var list = []; |
---|
102 | |
---|
103 | if(items){ |
---|
104 | for(var i = 0, item; item = items[i]; i++){ |
---|
105 | list.push(new ddcd._BoundItem(item, store)); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | context[this.alias] = list; |
---|
110 | return buffer; |
---|
111 | }, |
---|
112 | unrender: function(context, buffer){ |
---|
113 | return buffer; |
---|
114 | }, |
---|
115 | clone: function(){ |
---|
116 | return this; |
---|
117 | } |
---|
118 | }); |
---|
119 | |
---|
120 | lang.mixin(ddcd, { |
---|
121 | _get: function(key){ |
---|
122 | if(this.length){ |
---|
123 | return (this[0] instanceof ddcd._BoundItem) ? this[0].get(key) : this[0][key]; |
---|
124 | } |
---|
125 | }, |
---|
126 | bind_data: function(parser, token){ |
---|
127 | // summary: Turns a list of data store items into DTL compatible items |
---|
128 | // example: |
---|
129 | // `contextItems` and `contextStore` should be an item list |
---|
130 | // and a data store that get assigned to `newVariable` |
---|
131 | // |
---|
132 | // | {% bind_data contextItems to contextStore as newVariable %} |
---|
133 | var parts = token.contents.split(); |
---|
134 | |
---|
135 | if(parts[2] != 'to' || parts[4] != 'as' || !parts[5]){ |
---|
136 | throw new Error("data_bind expects the format: 'data_bind items to store as varName'"); |
---|
137 | } |
---|
138 | |
---|
139 | return new ddcd.BindDataNode(parts[1], null, parts[3], parts[5]); |
---|
140 | }, |
---|
141 | bind_query: function(parser, token){ |
---|
142 | // summary: Queries a data store and makes the returned items DTL compatible |
---|
143 | // example: |
---|
144 | // You can only use this with data stores that work in a synchronous |
---|
145 | // way (meaning that `onComplete` is fired during the `fetch` call). |
---|
146 | // A `sync` flag is sent to the fetch call so that stores that usually |
---|
147 | // work asynchronously make themselves syncrhonous if possible. |
---|
148 | // | {% bind_query contextQuery to contextStore as newVariable %} |
---|
149 | var parts = token.contents.split(); |
---|
150 | |
---|
151 | if(parts[2] != 'to' || parts[4] != 'as' || !parts[5]){ |
---|
152 | throw new Error("data_bind expects the format: 'bind_query query to store as varName'"); |
---|
153 | } |
---|
154 | |
---|
155 | return new ddcd.BindDataNode(null, parts[1], parts[3], parts[5]); |
---|
156 | } |
---|
157 | }); |
---|
158 | ddcd._get.safe = true; |
---|
159 | |
---|
160 | dd.register.tags("dojox.dtl.contrib", { |
---|
161 | "data": ["bind_data", "bind_query"] |
---|
162 | }); |
---|
163 | return dojox.dtl.contrib.data; |
---|
164 | }); |
---|