1 | define([ |
---|
2 | "dojo/_base/Deferred", |
---|
3 | "dojo/_base/declare" |
---|
4 | ], function(Deferred, declare){ |
---|
5 | |
---|
6 | // module: |
---|
7 | // dojox/mobile/_StoreMixin |
---|
8 | |
---|
9 | return declare("dojox.mobile._StoreMixin", null, { |
---|
10 | // summary: |
---|
11 | // Mixin for widgets to enable dojo/store data store. |
---|
12 | // description: |
---|
13 | // By mixing this class into a widget, it can get data through a |
---|
14 | // dojo/store data store. The widget must implement the following |
---|
15 | // methods to handle the retrieved data: |
---|
16 | // |
---|
17 | // - onComplete(/*Array*/items), onError(/*Object*/errorData), |
---|
18 | // - onUpdate(/*Object*/item, /*Number*/insertedInto), and |
---|
19 | // - onDelete(/*Object*/item, /*Number*/removedFrom). |
---|
20 | |
---|
21 | // store: Object |
---|
22 | // Reference to data provider object used by this widget. |
---|
23 | store: null, |
---|
24 | |
---|
25 | // query: Object |
---|
26 | // A query that can be passed to 'store' to initially filter the items. |
---|
27 | query: null, |
---|
28 | |
---|
29 | // queryOptions: Object |
---|
30 | // An optional parameter for the query. |
---|
31 | queryOptions: null, |
---|
32 | |
---|
33 | // labelProperty: String |
---|
34 | // A property name (a property in the dojo/store item) that specifies that item's label. |
---|
35 | labelProperty: "label", |
---|
36 | |
---|
37 | // childrenProperty: String |
---|
38 | // A property name (a property in the dojo/store item) that specifies that item's children. |
---|
39 | childrenProperty: "children", |
---|
40 | |
---|
41 | setStore: function(/*dojo/store/api/Store*/store, /*String*/query, /*Object*/queryOptions){ |
---|
42 | // summary: |
---|
43 | // Sets the store to use with this widget. |
---|
44 | if(store === this.store){ return null; } |
---|
45 | if(store){ |
---|
46 | store.getValue = function(item, property){ |
---|
47 | return item[property]; |
---|
48 | }; |
---|
49 | } |
---|
50 | this.store = store; |
---|
51 | this._setQuery(query, queryOptions); |
---|
52 | return this.refresh(); |
---|
53 | }, |
---|
54 | |
---|
55 | setQuery: function(/*String*/query, /*Object*/queryOptions){ |
---|
56 | this._setQuery(query, queryOptions); |
---|
57 | return this.refresh(); |
---|
58 | }, |
---|
59 | |
---|
60 | _setQuery: function(/*String*/query, /*Object*/queryOptions){ |
---|
61 | // tags: |
---|
62 | // private |
---|
63 | this.query = query; |
---|
64 | this.queryOptions = queryOptions || this.queryOptions; |
---|
65 | }, |
---|
66 | |
---|
67 | refresh: function(){ |
---|
68 | // summary: |
---|
69 | // Fetches the data and generates the list items. |
---|
70 | if(!this.store){ return null; } |
---|
71 | var _this = this; |
---|
72 | var promise = this.store.query(this.query, this.queryOptions); |
---|
73 | Deferred.when(promise, function(results){ |
---|
74 | if(results.items){ |
---|
75 | results = results.items; // looks like dojo/data style items array |
---|
76 | } |
---|
77 | if(promise.observe){ |
---|
78 | if(_this._observe_h){ |
---|
79 | _this._observe_h.remove(); |
---|
80 | } |
---|
81 | _this._observe_h = promise.observe(function(object, previousIndex, newIndex){ |
---|
82 | if(previousIndex != -1){ |
---|
83 | if(newIndex != previousIndex){ |
---|
84 | // item removed or moved |
---|
85 | _this.onDelete(object, previousIndex); |
---|
86 | if(newIndex != -1){ |
---|
87 | if (_this.onAdd) { |
---|
88 | // new widget with onAdd method defined |
---|
89 | _this.onAdd(object, newIndex); |
---|
90 | } else { |
---|
91 | // TODO remove in 2.0 |
---|
92 | // compatibility with 1.8: onAdd did not exist, add was handled by onUpdate |
---|
93 | _this.onUpdate(object, newIndex); |
---|
94 | } |
---|
95 | } |
---|
96 | }else{ |
---|
97 | // item modified |
---|
98 | // if onAdd is not defined, we are "bug compatible" with 1.8 and we do nothing. |
---|
99 | // TODO remove test in 2.0 |
---|
100 | if(_this.onAdd){ |
---|
101 | _this.onUpdate(object, newIndex); |
---|
102 | } |
---|
103 | } |
---|
104 | }else if(newIndex != -1){ |
---|
105 | // item added |
---|
106 | if(_this.onAdd){ |
---|
107 | // new widget with onAdd method defined |
---|
108 | _this.onAdd(object, newIndex); |
---|
109 | }else{ |
---|
110 | // TODO remove in 2.0 |
---|
111 | // compatibility with 1.8: onAdd did not exist, add was handled by onUpdate |
---|
112 | _this.onUpdate(object, newIndex); |
---|
113 | } |
---|
114 | } |
---|
115 | }, true); // we want to be notified of updates |
---|
116 | } |
---|
117 | _this.onComplete(results); |
---|
118 | }, function(error){ |
---|
119 | _this.onError(error); |
---|
120 | }); |
---|
121 | return promise; |
---|
122 | }, |
---|
123 | |
---|
124 | destroy: function(){ |
---|
125 | if(this._observe_h){ |
---|
126 | this._observe_h = this._observe_h.remove(); |
---|
127 | } |
---|
128 | this.inherited(arguments); |
---|
129 | } |
---|
130 | |
---|
131 | /*===== |
---|
132 | // Subclass MUST implement the following methods. |
---|
133 | |
---|
134 | , onComplete: function(items){ |
---|
135 | // summary: |
---|
136 | // A handler that is called after the fetch completes. |
---|
137 | }, |
---|
138 | |
---|
139 | onError: function(errorData){ |
---|
140 | // summary: |
---|
141 | // An error handler. |
---|
142 | }, |
---|
143 | |
---|
144 | onUpdate: function(item, insertedInto){ |
---|
145 | // summary: |
---|
146 | // Called when an existing data item has been modified in the store. |
---|
147 | // Note: for compatibility with previous versions where only onUpdate was present, |
---|
148 | // if onAdd is not defined, onUpdate will be called instead. |
---|
149 | }, |
---|
150 | |
---|
151 | onDelete: function(item, removedFrom){ |
---|
152 | // summary: |
---|
153 | // Called when a data item has been removed from the store. |
---|
154 | }, |
---|
155 | |
---|
156 | // Subclass should implement the following methods. |
---|
157 | |
---|
158 | onAdd: function(item, insertedInto){ |
---|
159 | // summary: |
---|
160 | // Called when a new data item has been added to the store. |
---|
161 | // Note: for compatibility with previous versions where this function did not exist, |
---|
162 | // if onAdd is not defined, onUpdate will be called instead. |
---|
163 | } |
---|
164 | =====*/ |
---|
165 | }); |
---|
166 | }); |
---|