Debian Codegraph @

dojox/mvc/EditStoreRefController.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "dojo/when",
5 "./getPlainValue",
6 "./EditModelRefController",
7 "./StoreRefController"
8 ], function(declare, lang, when, getPlainValue, EditModelRefController, StoreRefController){
9 return declare("dojox.mvc.EditStoreRefController", [StoreRefController, EditModelRefController], {
10 // summary:
11 // A child class of dojox/mvc/StoreRefController, managing edits.
12 // description:
13 // In addition to what dojox/mvc/StoreRefController does, the commit() method sends the data model as well as the removed entries in array to the data store.
14 // 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.
15 // example:
16 // The check box refers to "value" property in the controller (with "ctrl" ID).
17 // The controller provides the "value" property, from the data coming from data store ("store" property in the controller), using the first one in array.
18 // Two seconds later, the check box changes from unchecked to checked.
19 // The change is committed to the data store, which is reflected to dojo/store/Observable callback.
20 // | <html>
21 // | <head>
22 // | <script src="/path/to/dojo-toolkit/dojo/dojo.js" type="text/javascript" data-dojo-config="parseOnLoad: 0"></script>
23 // | <script type="text/javascript">
24 // | require([
25 // | "dojo/dom", "dojo/parser", "dojo/when", "dojo/store/Observable", "dojo/store/Memory", "dijit/registry", "dojo/domReady!"
26 // | ], function(ddom, parser, when, Observable, Memory, registry){
27 // | store = Observable(new Memory({data: [{id: "Foo", value: false}]}));
28 // | when(parser.parse(), function(){
29 // | registry.byId("ctrl").queryStore().observe(function(object, previousIndex, newIndex){
30 // | alert("ID: " + object.id + ", value: " + object.value);
31 // | }, true);
32 // | var count = 0;
33 // | var h = setInterval(function(){
34 // | ddom.byId("check").click();
35 // | registry.byId("ctrl").commit();
36 // | if(++count >= 2){ clearInterval(h); }
37 // | }, 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/EditStoreRefController" data-dojo-mixins="dojox/mvc/ListController"
45 // | data-dojo-props="store: store, cursorIndex: 0"></span>
46 // | <input id="check" type="checkbox" data-dojo-type="dijit/form/CheckBox" data-dojo-props="checked: at('widget:ctrl', 'value')">
47 // | </body>
48 // | </html>
49
50 // getPlainValueOptions: dojox/mvc/getPlainValueOptions
51 // The options to get plain value from stateful object.
52 getPlainValueOptions: null,
53
54 // _removals: Object[]
55 // The list of removed elements.
56 _removals: [],
57
58 // _resultsWatchHandle: dojox/mvc/StatefulArray.watchElements.handle
59 // The watch handle for model array elements.
60 _resultsWatchHandle: null,
61
62 // _refSourceModelProp: String
63 // The property name for the data model, that serves as the data source.
64 _refSourceModelProp: "sourceModel",
65
66 queryStore: function(/*Object*/ query, /*dojo/store/api/Store.QueryOptions?*/ options){
67 // summary:
68 // Queries the store for objects.
69 // query: Object
70 // The query to use for retrieving objects from the store.
71 // options: dojo/store/api/Store.QueryOptions?
72 // The optional arguments to apply to the resultset.
73 // returns: dojo/store/api/Store.QueryResults
74 // The results of the query, extended with iterative methods.
75
76 if(!(this.store || {}).query){ return; }
77 if(this._resultsWatchHandle){ this._resultsWatchHandle.unwatch(); }
78 this._removals = [];
79 var _self = this,
80 queryResult = this.inherited(arguments),
81 result = when(queryResult, function(results){
82 if(_self._beingDestroyed){ return; }
83 if(lang.isArray(results)){
84 _self._resultsWatchHandle = results.watchElements(function(idx, removals, adds){
85 [].push.apply(_self._removals, removals);
86 });
87 }
88 return results;
89 });
90 if(result.then){
91 result = lang.delegate(result);
92 }
93 // For dojo/store/Observable, which adds a function to query result
94 for(var s in queryResult){
95 if(isNaN(s) && queryResult.hasOwnProperty(s) && lang.isFunction(queryResult[s])){
96 result[s] = queryResult[s];
97 }
98 }
99 return result;
100 },
101
102 getStore: function(/*Number*/ id, /*Object*/ options){
103 // summary:
104 // Retrieves an object by its identity.
105 // id: Number
106 // The identity to use to lookup the object.
107 // options: Object
108 // The options for dojo/store/*/get().
109 // returns: Object
110 // The object in the store that matches the given id.
111
112 if(this._resultsWatchHandle){ this._resultsWatchHandle.unwatch(); }
113 return this.inherited(arguments);
114 },
115
116 commit: function(){
117 // summary:
118 // Send the change back to the data source.
119
120 if(this._removals){
121 for(var i = 0; i < this._removals.length; i++){
122 this.store.remove(this.store.getIdentity(this._removals[i]));
123 }
124 this._removals = [];
125 }
126 var data = getPlainValue(this.get(this._refEditModelProp), this.getPlainValueOptions);
127 if(lang.isArray(data)){
128 for(var i = 0; i < data.length; i++){
129 this.store.put(data[i]);
130 }
131 }else{
132 this.store.put(data);
133 }
134 this.inherited(arguments);
135 },
136
137 reset: function(){
138 // summary:
139 // Change the model back to its original state.
140
141 this.inherited(arguments);
142 this._removals = [];
143 },
144
145 destroy: function(){
146 // summary:
147 // Clean up model watch handle as this object is destroyed.
148
149 if(this._resultsWatchHandle){ this._resultsWatchHandle.unwatch(); }
150 this.inherited(arguments);
151 }
152 });
153 });