1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/when", |
---|
5 | "./getStateful", |
---|
6 | "./ModelRefController" |
---|
7 | ], function(declare, lang, when, getStateful, ModelRefController){ |
---|
8 | return declare("dojox.mvc.StoreRefController", ModelRefController, { |
---|
9 | // summary: |
---|
10 | // A child class of dojox.mvc.ModelRefController, which keeps a reference to Dojo Object Store (in store property). |
---|
11 | // description: |
---|
12 | // Has several methods to work with the store: |
---|
13 | // |
---|
14 | // - queryStore(): Runs query() against the store, and creates a data model from retrieved data |
---|
15 | // - getStore(): Runs get() against the store, and creates a data model from retrieved data |
---|
16 | // - putStore(): Runs put() against the store |
---|
17 | // - addStore(): Runs add() against the store |
---|
18 | // - removeStore(): Runs remove() against the store |
---|
19 | // |
---|
20 | // dojo.Stateful get()/set()/watch() interfaces in dojox.mvc.StoreRefController will work with the data model from queryStore() or getStore(). |
---|
21 | // |
---|
22 | // NOTE - If this class is used with a widget by data-dojo-mixins, make sure putting the widget in data-dojo-type and putting this class to data-dojo-mixins. |
---|
23 | // example: |
---|
24 | // The text box refers to "value" property in the controller (with "ctrl" ID). |
---|
25 | // The controller provides the "value" property, from the data coming from data store ("store" property in the controller). |
---|
26 | // Two seconds later, the text box changes from "Foo" to "Bar" as the controller gets new data from data store. |
---|
27 | // | <html> |
---|
28 | // | <head> |
---|
29 | // | <script src="/path/to/dojo-toolkit/dojo/dojo.js" type="text/javascript" data-dojo-config="parseOnLoad: 0"></script> |
---|
30 | // | <script type="text/javascript"> |
---|
31 | // | require([ |
---|
32 | // | "dojo/parser", "dojo/when", "dojo/store/Memory", "dijit/registry", "dojo/domReady!" |
---|
33 | // | ], function(parser, when, Memory, registry){ |
---|
34 | // | store = new Memory({data: [{id: "Foo", value: "Foo"}, {id: "Bar", value: "Bar"}]}); |
---|
35 | // | when(parser.parse(), function(){ |
---|
36 | // | registry.byId("ctrl").getStore("Foo"); |
---|
37 | // | setTimeout(function(){ registry.byId("ctrl").getStore("Bar"); }, 2000); |
---|
38 | // | }); |
---|
39 | // | }); |
---|
40 | // | </script> |
---|
41 | // | </head> |
---|
42 | // | <body> |
---|
43 | // | <script type="dojo/require">at: "dojox/mvc/at"</script> |
---|
44 | // | <span id="ctrl" data-dojo-type="dojox.mvc.StoreRefController" data-dojo-props="store: store"></span> |
---|
45 | // | <input type="text" data-dojo-type="dijit/form/TextBox" data-dojo-props="value: at('widget:ctrl', 'value')"> |
---|
46 | // | </body> |
---|
47 | // | </html> |
---|
48 | |
---|
49 | // store: dojo/store/* |
---|
50 | // The Dojo Object Store in use. |
---|
51 | store: null, |
---|
52 | |
---|
53 | // getStatefulOptions: dojox.mvc.getStatefulOptions |
---|
54 | // The options to get stateful object from plain value. |
---|
55 | getStatefulOptions: null, |
---|
56 | |
---|
57 | // _refSourceModelProp: String |
---|
58 | // The property name for the data model, that serves as the data source. |
---|
59 | _refSourceModelProp: "model", |
---|
60 | |
---|
61 | queryStore: function(/*Object*/ query, /*dojo/store/api/Store.QueryOptions?*/ options){ |
---|
62 | // summary: |
---|
63 | // Queries the store for objects. |
---|
64 | // query: Object |
---|
65 | // The query to use for retrieving objects from the store. |
---|
66 | // options: dojo/store/api/Store.QueryOptions? |
---|
67 | // The optional arguments to apply to the resultset. |
---|
68 | // returns: dojo/store/api/Store.QueryResults |
---|
69 | // The results of the query, extended with iterative methods. |
---|
70 | |
---|
71 | if(!(this.store || {}).query){ return; } |
---|
72 | if(this._queryObserveHandle){ this._queryObserveHandle.cancel(); } |
---|
73 | |
---|
74 | var _self = this, |
---|
75 | queryResult = this.store.query(query, options), |
---|
76 | result = when(queryResult, function(results){ |
---|
77 | if(_self._beingDestroyed){ return; } |
---|
78 | results = getStateful(results, _self.getStatefulOptions); |
---|
79 | _self.set(_self._refSourceModelProp, results); |
---|
80 | return results; |
---|
81 | }); |
---|
82 | if(result.then){ |
---|
83 | result = lang.delegate(result); |
---|
84 | } |
---|
85 | // For dojo/store/Observable, which adds a function to query result |
---|
86 | for(var s in queryResult){ |
---|
87 | if(isNaN(s) && queryResult.hasOwnProperty(s) && lang.isFunction(queryResult[s])){ |
---|
88 | result[s] = queryResult[s]; |
---|
89 | } |
---|
90 | } |
---|
91 | return result; |
---|
92 | }, |
---|
93 | |
---|
94 | getStore: function(/*Number*/ id, /*Object*/ options){ |
---|
95 | // summary: |
---|
96 | // Retrieves an object by its identity. |
---|
97 | // id: Number |
---|
98 | // The identity to use to lookup the object. |
---|
99 | // options: Object |
---|
100 | // The options for dojo/store.*.get(). |
---|
101 | // returns: Object |
---|
102 | // The object in the store that matches the given id. |
---|
103 | |
---|
104 | if(!(this.store || {}).get){ return; } |
---|
105 | if(this._queryObserveHandle){ this._queryObserveHandle.cancel(); } |
---|
106 | var _self = this; |
---|
107 | result = when(this.store.get(id, options), function(result){ |
---|
108 | if(_self._beingDestroyed){ return; } |
---|
109 | result = getStateful(result, _self.getStatefulOptions); |
---|
110 | _self.set(_self._refSourceModelProp, result); |
---|
111 | return result; |
---|
112 | }); |
---|
113 | return result; |
---|
114 | }, |
---|
115 | |
---|
116 | putStore: function(/*Object*/ object, /*dojo/store/api/Store.PutDirectives?*/ options){ |
---|
117 | // summary: |
---|
118 | // Stores an object. |
---|
119 | // object: Object |
---|
120 | // The object to store. |
---|
121 | // options: dojo/store/api/Store.PutDirectives? |
---|
122 | // Additional metadata for storing the data. Includes an "id" property if a specific id is to be used. |
---|
123 | // returns: Number |
---|
124 | |
---|
125 | if(!(this.store || {}).put){ return; } |
---|
126 | return this.store.put(object, options); |
---|
127 | }, |
---|
128 | |
---|
129 | addStore: function(object, options){ |
---|
130 | // summary: |
---|
131 | // Creates an object, throws an error if the object already exists. |
---|
132 | // object: Object |
---|
133 | // The object to store. |
---|
134 | // options: dojo/store/api/Store.PutDirectives? |
---|
135 | // Additional metadata for storing the data. Includes an "id" property if a specific id is to be used. |
---|
136 | // returns: Number |
---|
137 | |
---|
138 | if(!(this.store || {}).add){ return; } |
---|
139 | return this.store.add(object, options); |
---|
140 | }, |
---|
141 | |
---|
142 | removeStore: function(/*Number*/ id, /*Object*/ options){ |
---|
143 | // summary: |
---|
144 | // Deletes an object by its identity |
---|
145 | // id: Number |
---|
146 | // The identity to use to delete the object |
---|
147 | // options: Object |
---|
148 | // The options for dojo/store/*.remove(). |
---|
149 | // returns: Boolean |
---|
150 | // Returns true if an object was removed, falsy (undefined) if no object matched the id. |
---|
151 | |
---|
152 | if(!(this.store || {}).remove){ return; } |
---|
153 | return this.store.remove(id, options); |
---|
154 | } |
---|
155 | }); |
---|
156 | }); |
---|