source: Dev/trunk/src/client/dojox/dtl/contrib/data.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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