extjs-mvc 0.4.0.j → 0.4.0.k

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. data/Rakefile +1 -1
  2. data/VERSION +1 -1
  3. data/lib/extjs-mvc.rb +0 -14
  4. data/lib/extjs-mvc/api.rb +14 -27
  5. data/lib/extjs-mvc/src/App.js +219 -0
  6. data/lib/extjs-mvc/src/MVC.js +260 -0
  7. data/lib/extjs-mvc/src/Presenter.js +52 -0
  8. data/lib/extjs-mvc/src/README.rdoc +69 -0
  9. data/lib/extjs-mvc/src/controller/Controller.js +278 -0
  10. data/lib/extjs-mvc/src/controller/CrudController.js +460 -0
  11. data/lib/extjs-mvc/src/lib/Array.js +26 -0
  12. data/lib/extjs-mvc/src/lib/Booter.js +417 -0
  13. data/lib/extjs-mvc/src/lib/ClassManager.js +191 -0
  14. data/lib/extjs-mvc/src/lib/ControllerClassManager.js +95 -0
  15. data/lib/extjs-mvc/src/lib/Dependencies.js +44 -0
  16. data/lib/extjs-mvc/src/lib/DispatchMatcher.js +98 -0
  17. data/lib/extjs-mvc/src/lib/Dispatcher.js +129 -0
  18. data/lib/extjs-mvc/src/lib/Environment.js +43 -0
  19. data/lib/extjs-mvc/src/lib/Inflector.js +155 -0
  20. data/lib/extjs-mvc/src/lib/ModelClassManager.js +19 -0
  21. data/lib/extjs-mvc/src/lib/Route.js +139 -0
  22. data/lib/extjs-mvc/src/lib/Router.js +282 -0
  23. data/lib/extjs-mvc/src/lib/String.js +94 -0
  24. data/lib/extjs-mvc/src/lib/ViewClassManager.js +229 -0
  25. data/lib/extjs-mvc/src/lib/notes.txt +32 -0
  26. data/lib/extjs-mvc/src/model/AdapterManager.js +30 -0
  27. data/lib/extjs-mvc/src/model/Association.js +26 -0
  28. data/lib/extjs-mvc/src/model/Base.js +63 -0
  29. data/lib/extjs-mvc/src/model/BelongsToAssociation.js +116 -0
  30. data/lib/extjs-mvc/src/model/Cache.js +131 -0
  31. data/lib/extjs-mvc/src/model/HasManyAssociation.js +160 -0
  32. data/lib/extjs-mvc/src/model/Model.js +331 -0
  33. data/lib/extjs-mvc/src/model/UrlBuilder.js +106 -0
  34. data/lib/extjs-mvc/src/model/adapters/AbstractAdapter.js +296 -0
  35. data/lib/extjs-mvc/src/model/adapters/MemoryAdapter.js +103 -0
  36. data/lib/extjs-mvc/src/model/adapters/RESTAdapter.js +345 -0
  37. data/lib/extjs-mvc/src/model/adapters/RESTJSONAdapter.js +68 -0
  38. data/lib/extjs-mvc/src/model/adapters/notes.txt +42 -0
  39. data/lib/extjs-mvc/src/model/associations/Association.js +192 -0
  40. data/lib/extjs-mvc/src/model/associations/notes.txt +87 -0
  41. data/lib/extjs-mvc/src/model/validations/Errors.js +136 -0
  42. data/lib/extjs-mvc/src/model/validations/Plugin.js +139 -0
  43. data/lib/extjs-mvc/src/model/validations/Validations.js +276 -0
  44. data/lib/extjs-mvc/src/notes/Charts.graffle +0 -0
  45. data/lib/extjs-mvc/src/overrides/Ext.Component.js +21 -0
  46. data/lib/extjs-mvc/src/overrides/Ext.extend.js +142 -0
  47. data/lib/extjs-mvc/src/spec/Array.spec.js +15 -0
  48. data/lib/extjs-mvc/src/spec/ExtMVC.spec.js +65 -0
  49. data/lib/extjs-mvc/src/spec/Model.spec.js +370 -0
  50. data/lib/extjs-mvc/src/spec/OS.spec.js +83 -0
  51. data/lib/extjs-mvc/src/spec/Router.spec.js +99 -0
  52. data/lib/extjs-mvc/src/spec/SpecHelper.js +106 -0
  53. data/lib/extjs-mvc/src/spec/String.spec.js +83 -0
  54. data/lib/extjs-mvc/src/spec/model/AbstractAdapter.spec.js +49 -0
  55. data/lib/extjs-mvc/src/spec/model/Associations.spec.js +99 -0
  56. data/lib/extjs-mvc/src/spec/model/Cache.spec.js +5 -0
  57. data/lib/extjs-mvc/src/spec/model/RESTAdapter.spec.js +19 -0
  58. data/lib/extjs-mvc/src/spec/model/ValidationErrors.spec.js +64 -0
  59. data/lib/extjs-mvc/src/spec/model/Validations.spec.js +166 -0
  60. data/lib/extjs-mvc/src/spec/model/ValidationsPlugin.spec.js +108 -0
  61. data/lib/extjs-mvc/src/spec/suite.html +60 -0
  62. data/lib/extjs-mvc/src/specs-old/JSSpec.css +216 -0
  63. data/lib/extjs-mvc/src/specs-old/JSSpec.js +1512 -0
  64. data/lib/extjs-mvc/src/specs-old/all.html +66 -0
  65. data/lib/extjs-mvc/src/specs-old/base.js +14 -0
  66. data/lib/extjs-mvc/src/specs-old/controller.js +17 -0
  67. data/lib/extjs-mvc/src/specs-old/diff_match_patch.js +1 -0
  68. data/lib/extjs-mvc/src/specs-old/model.js +70 -0
  69. data/lib/extjs-mvc/src/specs-old/route.js +38 -0
  70. data/lib/extjs-mvc/src/specs-old/router.js +59 -0
  71. data/lib/extjs-mvc/src/specs-old/string.js +22 -0
  72. data/lib/extjs-mvc/src/testrunner/JSpecFormatter.js +111 -0
  73. data/lib/extjs-mvc/src/testrunner/TestClient.js +181 -0
  74. data/lib/extjs-mvc/src/testrunner/TestGrid.js +351 -0
  75. data/lib/extjs-mvc/src/testrunner/TestRunner.js +110 -0
  76. data/lib/extjs-mvc/src/testrunner/TestViewport.js +94 -0
  77. data/lib/extjs-mvc/src/vendor.yml +30 -0
  78. data/lib/extjs-mvc/src/vendor/ext-3.1.1/vendor.yml +16 -0
  79. data/lib/extjs-mvc/src/view/FormWindow.js +184 -0
  80. data/lib/extjs-mvc/src/view/HasManyEditorGridPanel.js +211 -0
  81. data/lib/extjs-mvc/src/view/scaffold/Edit.js +46 -0
  82. data/lib/extjs-mvc/src/view/scaffold/Index.js +561 -0
  83. data/lib/extjs-mvc/src/view/scaffold/New.js +20 -0
  84. data/lib/extjs-mvc/src/view/scaffold/ScaffoldFormPanel.js +255 -0
  85. data/lib/{vendor.yml → extjs-mvc/vendor.yml} +0 -0
  86. data/test/app/vendor/extjs-mvc/App.js +219 -0
  87. data/test/app/vendor/extjs-mvc/MVC.js +260 -0
  88. data/test/app/vendor/extjs-mvc/Presenter.js +52 -0
  89. data/test/app/vendor/extjs-mvc/README.rdoc +69 -0
  90. data/test/app/vendor/extjs-mvc/controller/Controller.js +278 -0
  91. data/test/app/vendor/extjs-mvc/controller/CrudController.js +460 -0
  92. data/test/app/vendor/extjs-mvc/lib/Array.js +26 -0
  93. data/test/app/vendor/extjs-mvc/lib/Booter.js +417 -0
  94. data/test/app/vendor/extjs-mvc/lib/ClassManager.js +191 -0
  95. data/test/app/vendor/extjs-mvc/lib/ControllerClassManager.js +95 -0
  96. data/test/app/vendor/extjs-mvc/lib/Dependencies.js +44 -0
  97. data/test/app/vendor/extjs-mvc/lib/DispatchMatcher.js +98 -0
  98. data/test/app/vendor/extjs-mvc/lib/Dispatcher.js +129 -0
  99. data/test/app/vendor/extjs-mvc/lib/Environment.js +43 -0
  100. data/test/app/vendor/extjs-mvc/lib/Inflector.js +155 -0
  101. data/test/app/vendor/extjs-mvc/lib/ModelClassManager.js +19 -0
  102. data/test/app/vendor/extjs-mvc/lib/Route.js +139 -0
  103. data/test/app/vendor/extjs-mvc/lib/Router.js +282 -0
  104. data/test/app/vendor/extjs-mvc/lib/String.js +94 -0
  105. data/test/app/vendor/extjs-mvc/lib/ViewClassManager.js +229 -0
  106. data/test/app/vendor/extjs-mvc/lib/notes.txt +32 -0
  107. data/test/app/vendor/extjs-mvc/model/AdapterManager.js +30 -0
  108. data/test/app/vendor/extjs-mvc/model/Association.js +26 -0
  109. data/test/app/vendor/extjs-mvc/model/Base.js +63 -0
  110. data/test/app/vendor/extjs-mvc/model/BelongsToAssociation.js +116 -0
  111. data/test/app/vendor/extjs-mvc/model/Cache.js +131 -0
  112. data/test/app/vendor/extjs-mvc/model/HasManyAssociation.js +160 -0
  113. data/test/app/vendor/extjs-mvc/model/Model.js +331 -0
  114. data/test/app/vendor/extjs-mvc/model/UrlBuilder.js +106 -0
  115. data/test/app/vendor/extjs-mvc/model/adapters/AbstractAdapter.js +296 -0
  116. data/test/app/vendor/extjs-mvc/model/adapters/MemoryAdapter.js +103 -0
  117. data/test/app/vendor/extjs-mvc/model/adapters/RESTAdapter.js +345 -0
  118. data/test/app/vendor/extjs-mvc/model/adapters/RESTJSONAdapter.js +68 -0
  119. data/test/app/vendor/extjs-mvc/model/adapters/notes.txt +42 -0
  120. data/test/app/vendor/extjs-mvc/model/associations/Association.js +192 -0
  121. data/test/app/vendor/extjs-mvc/model/associations/notes.txt +87 -0
  122. data/test/app/vendor/extjs-mvc/model/validations/Errors.js +136 -0
  123. data/test/app/vendor/extjs-mvc/model/validations/Plugin.js +139 -0
  124. data/test/app/vendor/extjs-mvc/model/validations/Validations.js +276 -0
  125. data/test/app/vendor/extjs-mvc/notes/Charts.graffle +0 -0
  126. data/test/app/vendor/extjs-mvc/overrides/Ext.Component.js +21 -0
  127. data/test/app/vendor/extjs-mvc/overrides/Ext.extend.js +142 -0
  128. data/test/app/vendor/extjs-mvc/spec/Array.spec.js +15 -0
  129. data/test/app/vendor/extjs-mvc/spec/ExtMVC.spec.js +65 -0
  130. data/test/app/vendor/extjs-mvc/spec/Model.spec.js +370 -0
  131. data/test/app/vendor/extjs-mvc/spec/OS.spec.js +83 -0
  132. data/test/app/vendor/extjs-mvc/spec/Router.spec.js +99 -0
  133. data/test/app/vendor/extjs-mvc/spec/SpecHelper.js +106 -0
  134. data/test/app/vendor/extjs-mvc/spec/String.spec.js +83 -0
  135. data/test/app/vendor/extjs-mvc/spec/model/AbstractAdapter.spec.js +49 -0
  136. data/test/app/vendor/extjs-mvc/spec/model/Associations.spec.js +99 -0
  137. data/test/app/vendor/extjs-mvc/spec/model/Cache.spec.js +5 -0
  138. data/test/app/vendor/extjs-mvc/spec/model/RESTAdapter.spec.js +19 -0
  139. data/test/app/vendor/extjs-mvc/spec/model/ValidationErrors.spec.js +64 -0
  140. data/test/app/vendor/extjs-mvc/spec/model/Validations.spec.js +166 -0
  141. data/test/app/vendor/extjs-mvc/spec/model/ValidationsPlugin.spec.js +108 -0
  142. data/test/app/vendor/extjs-mvc/spec/suite.html +60 -0
  143. data/test/app/vendor/extjs-mvc/specs-old/JSSpec.css +216 -0
  144. data/test/app/vendor/extjs-mvc/specs-old/JSSpec.js +1512 -0
  145. data/test/app/vendor/extjs-mvc/specs-old/all.html +66 -0
  146. data/test/app/vendor/extjs-mvc/specs-old/base.js +14 -0
  147. data/test/app/vendor/extjs-mvc/specs-old/controller.js +17 -0
  148. data/test/app/vendor/extjs-mvc/specs-old/diff_match_patch.js +1 -0
  149. data/test/app/vendor/extjs-mvc/specs-old/model.js +70 -0
  150. data/test/app/vendor/extjs-mvc/specs-old/route.js +38 -0
  151. data/test/app/vendor/extjs-mvc/specs-old/router.js +59 -0
  152. data/test/app/vendor/extjs-mvc/specs-old/string.js +22 -0
  153. data/test/app/vendor/extjs-mvc/testrunner/JSpecFormatter.js +111 -0
  154. data/test/app/vendor/extjs-mvc/testrunner/TestClient.js +181 -0
  155. data/test/app/vendor/extjs-mvc/testrunner/TestGrid.js +351 -0
  156. data/test/app/vendor/extjs-mvc/testrunner/TestRunner.js +110 -0
  157. data/test/app/vendor/extjs-mvc/testrunner/TestViewport.js +94 -0
  158. data/test/app/vendor/extjs-mvc/vendor.yml +30 -0
  159. data/test/app/vendor/extjs-mvc/vendor/ext-3.1.1/vendor.yml +16 -0
  160. data/test/app/vendor/extjs-mvc/view/FormWindow.js +184 -0
  161. data/test/app/vendor/extjs-mvc/view/HasManyEditorGridPanel.js +211 -0
  162. data/test/app/vendor/extjs-mvc/view/scaffold/Edit.js +46 -0
  163. data/test/app/vendor/extjs-mvc/view/scaffold/Index.js +561 -0
  164. data/test/app/vendor/extjs-mvc/view/scaffold/New.js +20 -0
  165. data/test/app/vendor/extjs-mvc/view/scaffold/ScaffoldFormPanel.js +255 -0
  166. data/test/helper.rb +7 -1
  167. data/test/test_extjs-mvc.rb +46 -0
  168. metadata +167 -7
  169. data/test/test_extjs-mvc-gem.rb +0 -7
@@ -0,0 +1,345 @@
1
+ /**
2
+ * @class ExtMVC.model.plugin.adapter.RESTAdapter
3
+ * @extends ExtMVC.model.plugin.adapter.Abstract
4
+ * An adapter which hooks into a RESTful server side API for its data storage. This is the recommended
5
+ * adapter to use on MVC applications.
6
+ * <h2>Usage</h2>
7
+ * Say we have a User model defined:
8
+ <pre><code>
9
+ ExtMVC.model.define("User", {
10
+ fields: [{name: 'id', type: 'int'}, {name: 'name', type: 'string'}]
11
+ });
12
+ var user = new User({id: 1, name: 'Saul Tigh'});
13
+ </code></pre>
14
+ * If this model uses the REST Adapter, the following methods are made available to it. Each fires the AJAX request
15
+ * indicated in the comment next to it:
16
+ <pre><code>
17
+ user.destroy(); //DELETE /users/1
18
+ user.save(); //PUT /users/1 with {id: 1, name: 'Saul Tigh'} as the request payload
19
+ user.update({name: 'Bill Adama'})l //PUT /users/1/ with {id: 1, name: 'Bill Adama'} as the request payload
20
+ </code></pre>
21
+ * In addition, the following methods are made available to the User class object:
22
+ <pre><code>
23
+ User.destroy(10); //DELETE /users/1
24
+
25
+ User.find(10, {
26
+ success: function(instance) {
27
+ console.log("Asyncronously loaded User 10 from /users/10 using GET")
28
+ },
29
+ failure: function() {
30
+ console.log('Called if user 10 could not be found');
31
+ }
32
+ }); //GET /users/10
33
+
34
+ User.create({name: 'Gaius Baltar'}, {
35
+ success: function(instance) {
36
+ console.log('Gaius was created');
37
+ },
38
+ failure: function(errors) {
39
+ console.log('Called if Gaius could not be created');
40
+ console.log(errors);
41
+ }
42
+ }); //POST /users
43
+
44
+ User.build({name: 'Felix Gaeta'}); //same as new User({name: 'Felix Gaeta'});
45
+ </code></pre>
46
+ */
47
+ ExtMVC.model.plugin.adapter.RESTAdapter = Ext.extend(ExtMVC.model.plugin.adapter.Abstract, {
48
+
49
+ /**
50
+ * @property createMethod
51
+ * @type String
52
+ * The HTTP verb to use when creating a new instance (defaults to 'POST')
53
+ */
54
+ createMethod: 'POST',
55
+
56
+ /**
57
+ * @property readMethod
58
+ * @type String
59
+ * The HTTP verb to use when reading data from the server (e.g. in find requests). Defaults to 'GET'
60
+ */
61
+ readMethod: 'GET',
62
+
63
+ /**
64
+ * @property updateMethod
65
+ * @type String
66
+ * The HTTP verb to use when updating an existing instance (defaults to 'PUT')
67
+ */
68
+ updateMethod: 'PUT',
69
+
70
+ /**
71
+ * @property destroyMethod
72
+ * @type String
73
+ * The HTTP verb to use when destroying an instance (defaults to 'DELETE')
74
+ */
75
+ destroyMethod: 'DELETE',
76
+
77
+ /**
78
+ * @property proxyType
79
+ * @type Function
80
+ * The type of Data Proxy to use (defaults to Ext.data.HttpProxy)
81
+ */
82
+ proxyType: Ext.data.HttpProxy,
83
+
84
+ /**
85
+ * Performs the actual save request. Uses POST for new records, PUT when updating existing ones
86
+ */
87
+ doSave: function(instance, options) {
88
+ if (typeof instance == 'undefined') throw new Error('No instance provided to REST Adapter save');
89
+ options = options || {};
90
+
91
+ var successFn = options.success || Ext.emptyFn,
92
+ failureFn = options.failure || Ext.emptyFn;
93
+
94
+ delete options.success; delete options.failure;
95
+
96
+ Ext.Ajax.request(
97
+ Ext.apply({
98
+ url : this.instanceUrl(instance),
99
+ method : instance.newRecord() ? this.createMethod : this.updateMethod,
100
+ params : this.buildPostData(instance),
101
+
102
+ success: function(instance, userCallback, scope) {
103
+ var scope = scope || this;
104
+
105
+ return function(response, options) {
106
+ var jsonPath = instance.modelName.underscore(),
107
+ jsonData = Ext.decode(response.responseText)[jsonPath];
108
+
109
+ for (var key in jsonData) {
110
+ instance.set(key, jsonData[key]);
111
+ }
112
+
113
+ userCallback.call(scope, instance);
114
+ };
115
+ }(instance, successFn, options.scope),
116
+
117
+ failure: function(instance, userCallback, scope) {
118
+ var scope = options.scope || this;
119
+
120
+ return function(response, options) {
121
+ failureFn.call(scope, instance);
122
+ };
123
+ }(instance, failureFn, options.scope)
124
+ }, options)
125
+ );
126
+ },
127
+
128
+ /**
129
+ * Callback for save AJAX request. By default this reads server response data and populates the instance
130
+ * if the request was successful, adds errors if not
131
+ * @private
132
+ */
133
+ afterSave: function() {
134
+
135
+ },
136
+
137
+ /**
138
+ * Performs the actual find request.
139
+ * @private
140
+ * @param {Object} conditions An object containing find conditions. If a primaryKey is set this will be used
141
+ * to build the url for that particular instance, otherwise the collection url will be used
142
+ * @param {Object} options Callbacks (use callback, success and failure)
143
+ */
144
+ doFind: function(conditions, options, constructor) {
145
+ conditions = conditions || {}; options = options || {};
146
+
147
+ //if primary key is given, perform a single search
148
+ var single = (conditions.primaryKey !== undefined),
149
+ url = options.url || this.findUrl(conditions, constructor);
150
+
151
+ Ext.applyIf(options, {
152
+ conditions: conditions,
153
+ scope : this
154
+ });
155
+
156
+ var findMethod = single ? this.doSingleFind : this.doCollectionFind;
157
+ return findMethod.call(this, url, options, constructor);
158
+ },
159
+
160
+ /**
161
+ * Performs an HTTP DELETE request using Ext.Ajax.request
162
+ * @private
163
+ * @param {ExtMVC.model.Base} instance The model instance to destroy
164
+ * @param {Object} options Options object passed to Ext.Ajax.request
165
+ * @return {Number} The Ajax transaction ID
166
+ */
167
+ doDestroy: function(instance, options, constructor) {
168
+ var options = options || {};
169
+
170
+ if (typeof instance == 'undefined') throw new Error('No instance provided to REST Adapter destroy');
171
+
172
+ if (!(instance instanceof Ext.data.Record)) {
173
+ var id = parseInt(instance, 10);
174
+
175
+ instance = new constructor();
176
+ instance.set(constructor.prototype.primaryKey, id);
177
+ }
178
+
179
+ //if we were passed a success function, save it here so that we can call it with the instance later
180
+ var successFn = options.success || Ext.emptyFn;
181
+ delete options.success;
182
+
183
+ return Ext.Ajax.request(
184
+ Ext.applyIf(options, {
185
+ method: this.destroyMethod,
186
+ url: this.instanceUrl(instance),
187
+ success: function() {
188
+ successFn.call(options.scope || this, instance);
189
+ }
190
+ })
191
+ );
192
+ },
193
+
194
+ /**
195
+ * Loads a single instance of a model via an Ext.Ajax.request
196
+ * @private
197
+ * @param {String} url The url to load from
198
+ * @param {Object} options Options passed to Ext.Ajax.request
199
+ * @param {Function} constructor The constructor function used to instantiate the model instance
200
+ * @return {Number} The transaction ID of the Ext.Ajax.request
201
+ */
202
+ doSingleFind: function(url, options, constructor) {
203
+ //store references to user callbacks as we need to overwrite them in the request
204
+ var optionsCallback = options.callback,
205
+ successCallback = options.success,
206
+ failureCallback = options.failure;
207
+
208
+ delete options.callback; delete options.success; delete options.failure;
209
+
210
+ //need to make a local reference here as scope inside the Ext.data.Request block may not be 'this'
211
+ var decodeFunction = this.decodeSingleLoadResponse;
212
+
213
+ //helper function to cut down repetition in Ajax request callback
214
+ var callIf = function(callback, args) {
215
+ if (typeof callback == 'function') callback.apply(options.scope, args);
216
+ };
217
+
218
+ Ext.Ajax.request(
219
+ Ext.apply(options, {
220
+ callback: function(opts, success, response) {
221
+ if (success === true) {
222
+ var instance = new constructor(decodeFunction(response.responseText, constructor));
223
+
224
+ callIf(successCallback, [instance, opts, response]);
225
+ } else callIf(failureCallback, arguments);
226
+
227
+ //call the generic callback passed into options
228
+ callIf(optionsCallback, arguments);
229
+ }
230
+ }, this.buildProxyConfig(url))
231
+ );
232
+ },
233
+
234
+ /**
235
+ * @property storeConfig
236
+ * @type Object
237
+ * Default properties assigned to the Ext.data.Store used in find requests
238
+ */
239
+ storeConfig: {
240
+ autoLoad : true,
241
+ remoteSort: false
242
+ },
243
+
244
+ /**
245
+ * Specialised find for dealing with collections. Returns an Ext.data.Store
246
+ * @private
247
+ * @param {String} url The url to load the collection from
248
+ * @param {Object} options Options passed to the Store constructor
249
+ * @param {Function} constructor The constructor function used to instantiate the model instance
250
+ * @return {Ext.data.Store} A Store with the appropriate configuration to load this collection
251
+ */
252
+ doCollectionFind: function(url, options, constructor) {
253
+ Ext.applyIf(options, this.storeConfig);
254
+
255
+ if (options.conditions != undefined) {
256
+ Ext.applyIf(options, {
257
+ baseParams: options.conditions
258
+ });
259
+ }
260
+
261
+ return new Ext.data.Store(
262
+ Ext.applyIf(options, {
263
+ reader : constructor.prototype.getReader(),
264
+ proxy : new this.proxyType(this.buildProxyConfig(url))
265
+ })
266
+ );
267
+ },
268
+
269
+ /**
270
+ * Calculates the unique REST URL for a given model instance
271
+ * @param {ExtMVC.model.Base} instance The model instance
272
+ * @return {String} The url associated with this instance
273
+ */
274
+ instanceUrl: function(instance) {
275
+ if (instance.newRecord()) {
276
+ return String.format("/{0}", instance.tableName);
277
+ } else {
278
+ return String.format("/{0}/{1}", instance.tableName, instance.get(instance.primaryKey));
279
+ }
280
+ },
281
+
282
+ /**
283
+ * Calculates the REST URL for a given model collection. By default this just returns / followed by the table name
284
+ * @param {Function} constructor The model constructor function
285
+ */
286
+ collectionUrl: function(constructor) {
287
+ return String.format("/{0}", constructor.prototype.tableName);
288
+ },
289
+
290
+ /**
291
+ * Returns configuration data to be used by the DataProxy when loading records. Override to provide your own config
292
+ * @param {String} url The url the proxy should use. This is typically calculated elsewhere so must be provided
293
+ * @return {Object} Configuration for the proxy
294
+ */
295
+ buildProxyConfig: function(url) {
296
+ return {
297
+ url: url,
298
+ method: this.readMethod
299
+ };
300
+ },
301
+
302
+ /**
303
+ * Creates a params object suitable for sending as POST data to the server
304
+ * @param {ExtMVC.model.Base} instance The models instance to build post data for
305
+ * @return {Object} Params object to send to the server
306
+ */
307
+ buildPostData: function(instance) {
308
+ var data = {},
309
+ prefix = instance.modelName.underscore();
310
+
311
+ for (key in instance.data) {
312
+ data[prefix + '[' + key + ']'] = instance.data[key];
313
+ }
314
+
315
+ return data;
316
+ },
317
+
318
+ /**
319
+ * Decodes response text received from the server as the result of requesting data for a single record.
320
+ * By default this expects the data to be in the form {"model_name": {"key": "value", "key2", "value 2"}}
321
+ * and would return an object like {"key": "value", "key2", "value 2"}
322
+ * @param {String} responseText The raw response text
323
+ * @param {Function} constructor The constructor used to construct model instances. Useful for access to the prototype
324
+ * @return {Object} Decoded data suitable for use in a model constructor
325
+ */
326
+ decodeSingleLoadResponse: function(responseText, constructor) {
327
+ var tname = constructor.prototype.tableName;
328
+ return Ext.decode(responseText)[tname];
329
+ },
330
+
331
+ //private
332
+ findUrl: function(conditions, constructor) {
333
+ if (typeof(conditions) == 'object' && conditions.primaryKey) {
334
+ //find by ID
335
+ var instance = new constructor({});
336
+ instance.set(instance.primaryKey, conditions.primaryKey);
337
+ delete conditions.primaryKey;
338
+
339
+ return this.instanceUrl(instance);
340
+ } else {
341
+ //find by conditions
342
+ return this.collectionUrl(constructor);
343
+ }
344
+ }
345
+ });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @class ExtMVC.model.plugin.adapter.RESTJSONAdapter
3
+ * @extends ExtMVC.model.plugin.adapter.RESTAdapter
4
+ * An adapter which hooks into a RESTful server side API that expects JSON for its data storage
5
+ */
6
+ ExtMVC.model.plugin.adapter.RESTJSONAdapter = Ext.extend(ExtMVC.model.plugin.adapter.RESTAdapter, {
7
+
8
+ /**
9
+ * Performs the actual save request. Uses POST for new records, PUT when updating existing ones
10
+ * puts the data into jsonData for the request
11
+ */
12
+ doSave: function(instance, options) {
13
+ if (typeof instance == 'undefined') throw new Error('No instance provided to REST Adapter save');
14
+
15
+ Ext.applyIf(options || {}, {
16
+ jsonData: instance.data,
17
+ params : {}, //ensures that params aren't appended to the end of the url
18
+ headers : {
19
+ "Content-Type": "application/json"
20
+ }
21
+ });
22
+
23
+ ExtMVC.model.plugin.adapter.RESTJSONAdapter.superclass.doSave.apply(this, arguments);
24
+ },
25
+
26
+ /**
27
+ * Performs the actual destroy request. This simply adds an 'application/json' content type to the headers
28
+ */
29
+ doDestroy: function(instance, options, constructor) {
30
+ options = options || {};
31
+
32
+ Ext.applyIf(options, {
33
+ headers: {
34
+ "Content-Type": "application/json"
35
+ }
36
+ });
37
+
38
+ ExtMVC.model.plugin.adapter.RESTJSONAdapter.superclass.doDestroy.call(this, instance, options, constructor);
39
+ },
40
+
41
+ /**
42
+ * Decodes response text received from the server as the result of requesting data for a single record.
43
+ * By default this expects the data to be in the form {"model_name": {"key": "value", "key2", "value 2"}}
44
+ * and would return an object like {"key": "value", "key2", "value 2"}
45
+ * @param {String} responseText The raw response text
46
+ * @param {Function} constructor The constructor used to construct model instances. Useful for access to the prototype
47
+ * @return {Object} Decoded data suitable for use in a model constructor
48
+ */
49
+ decodeSingleLoadResponse: function(responseText, constructor) {
50
+ var tname = ExtMVC.Inflector.singularize(constructor.prototype.tableName);
51
+ return Ext.decode(responseText)[tname];
52
+ },
53
+
54
+ /**
55
+ * Returns configuration data to be used by the DataProxy when loading records. Override to provide your own config
56
+ * @param {String} url The url the proxy should use. This is typically calculated elsewhere
57
+ * @return {Object} Configuration for the proxy
58
+ */
59
+ buildProxyConfig: function(url) {
60
+ var defaults = ExtMVC.model.plugin.adapter.RESTJSONAdapter.superclass.buildProxyConfig.apply(this, arguments);
61
+
62
+ return Ext.apply(defaults, {
63
+ headers: {
64
+ "Content-Type": "application/json"
65
+ }
66
+ });
67
+ }
68
+ });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Method Collection Individual
3
+ * create yes yes (but different)
4
+ * build yes yes (might be different)
5
+ * find yes no
6
+ * loaded yes no
7
+ * count yes no
8
+ * destroy yes yes (but different)
9
+ */
10
+
11
+
12
+ // ExtMVC.model.Adapter.Abstract = {
13
+ // initialize: function(model) {
14
+ //
15
+ // },
16
+ //
17
+ // classMethods: {
18
+ // find: function(options) {
19
+ //
20
+ // },
21
+ //
22
+ // findById: function(id, options) {
23
+ // return this.findByField('id', id, options);
24
+ // },
25
+ //
26
+ // findByField: function(fieldName, matcher, options) {
27
+ //
28
+ // },
29
+ //
30
+ // findAll: function(options) {
31
+ //
32
+ // }
33
+ // },
34
+ //
35
+ // instanceMethods: {
36
+ // save: Ext.emptyFn,
37
+ //
38
+ // reload: Ext.emptyFn,
39
+ //
40
+ // destroy: Ext.emptyFn
41
+ // }
42
+ // };