|
| 1 | +/** |
| 2 | + Ember-REST.js |
| 3 | +
|
| 4 | + A simple library for RESTful resources in Ember.js |
| 5 | +
|
| 6 | + Copyright (c) 2012 Cerebris Corporation |
| 7 | +
|
| 8 | + Licensed under the MIT license: |
| 9 | + http://www.opensource.org/licenses/mit-license.php |
| 10 | +*/ |
| 11 | + |
| 12 | +/** |
| 13 | + A model class for RESTful resources |
| 14 | +
|
| 15 | + Extend this class and define the following properties: |
| 16 | +
|
| 17 | + * `resourceUrl` -- the base url of the resource (e.g. '/contacts'); |
| 18 | + will append '/id' for individual resources (required) |
| 19 | + * `resourceName` -- the name used to contain the serialized data in this |
| 20 | + object's JSON representation (required only for serialization) |
| 21 | + * `resourceProperties` -- an array of property names to be returned in this |
| 22 | + object's JSON representation (required only for serialization) |
| 23 | +
|
| 24 | + Because `resourceName` and `resourceProperties` are only used for |
| 25 | + serialization, they aren't required for read-only resources. |
| 26 | +
|
| 27 | + You may also wish to override / define the following methods: |
| 28 | +
|
| 29 | + * `serialize()` |
| 30 | + * `serializeProperty(prop)` |
| 31 | + * `deserialize(json)` |
| 32 | + * `deserializeProperty(prop, value)` |
| 33 | + * `validate()` |
| 34 | +*/ |
| 35 | +Ember.Resource = Ember.Object.extend({ |
| 36 | + resourceUrl: Ember.required(), |
| 37 | + |
| 38 | + /** |
| 39 | + Duplicate properties from another resource |
| 40 | +
|
| 41 | + * `source` -- an Ember.Resource object |
| 42 | + * `props` -- the array of properties to be duplicated; |
| 43 | + defaults to `resourceProperties` |
| 44 | + */ |
| 45 | + duplicateProperties: function(source, props) { |
| 46 | + var prop; |
| 47 | + |
| 48 | + if (props === undefined) props = this.resourceProperties; |
| 49 | + |
| 50 | + for(var i = 0; i < props.length; i++) { |
| 51 | + prop = props[i]; |
| 52 | + this.set(prop, source.get(prop)); |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + Generate this resource's JSON representation |
| 58 | +
|
| 59 | + Override this or `serializeProperty` to provide custom serialization |
| 60 | +
|
| 61 | + REQUIRED: `resourceProperties` and `resourceName` (see note above) |
| 62 | + */ |
| 63 | + serialize: function() { |
| 64 | + var name = this.resourceName, |
| 65 | + props = this.resourceProperties, |
| 66 | + prop, |
| 67 | + ret = {}; |
| 68 | + |
| 69 | + ret[name] = {}; |
| 70 | + for(var i = 0; i < props.length; i++) { |
| 71 | + prop = props[i]; |
| 72 | + ret[name][prop] = this.serializeProperty(prop); |
| 73 | + } |
| 74 | + return ret; |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + Generate an individual property's JSON representation |
| 79 | +
|
| 80 | + Override to provide custom serialization |
| 81 | + */ |
| 82 | + serializeProperty: function(prop) { |
| 83 | + return this.get(prop); |
| 84 | + }, |
| 85 | + |
| 86 | + /** |
| 87 | + Set this resource's properties from JSON |
| 88 | +
|
| 89 | + Override this or `deserializeProperty` to provide custom deserialization |
| 90 | + */ |
| 91 | + deserialize: function(json) { |
| 92 | + Ember.beginPropertyChanges(this); |
| 93 | + for(var prop in json) { |
| 94 | + if (json.hasOwnProperty(prop)) this.deserializeProperty(prop, json[prop]); |
| 95 | + } |
| 96 | + Ember.endPropertyChanges(this); |
| 97 | + return this; |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + Set an individual property from its value in JSON |
| 102 | +
|
| 103 | + Override to provide custom serialization |
| 104 | + */ |
| 105 | + deserializeProperty: function(prop, value) { |
| 106 | + this.set(prop, value); |
| 107 | + }, |
| 108 | + |
| 109 | + /** |
| 110 | + Load via ajax and deserialize |
| 111 | +
|
| 112 | + REQUIRED: `id` |
| 113 | + */ |
| 114 | + findResource: function() { |
| 115 | + var self = this; |
| 116 | + |
| 117 | + return jQuery.ajax({ |
| 118 | + url: this._resourceUrl(), |
| 119 | + dataType: 'json', |
| 120 | + type: 'GET' |
| 121 | + }).done( function(json) { |
| 122 | + self.deserialize(json); |
| 123 | + }); |
| 124 | + }, |
| 125 | + |
| 126 | + /** |
| 127 | + Create (if new) or update (if existing) record via ajax |
| 128 | +
|
| 129 | + Will call validate() if defined for this record |
| 130 | +
|
| 131 | + If successful, updates this record's id and other properties |
| 132 | + by calling `deserialize()` with the data returned. |
| 133 | +
|
| 134 | + REQUIRED: `properties` and `name` (see note above) |
| 135 | + */ |
| 136 | + saveResource: function() { |
| 137 | + var self = this; |
| 138 | + |
| 139 | + if (this.validate !== undefined) { |
| 140 | + var error = this.validate(); |
| 141 | + if (error) { |
| 142 | + return { |
| 143 | + fail: function(f) { f(error); return this; }, |
| 144 | + done: function() { return this; }, |
| 145 | + always: function(f) { f(); return this; } |
| 146 | + }; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return jQuery.ajax({ |
| 151 | + url: this._resourceUrl(), |
| 152 | + data: this.serialize(), |
| 153 | + dataType: 'json', |
| 154 | + type: (this.isNew() ? 'POST' : 'PUT') |
| 155 | + }).done( function(json) { |
| 156 | + // Update properties |
| 157 | + if (json) |
| 158 | + self.deserialize(json); |
| 159 | + }); |
| 160 | + }, |
| 161 | + |
| 162 | + /** |
| 163 | + Delete resource via ajax |
| 164 | + */ |
| 165 | + destroyResource: function() { |
| 166 | + return jQuery.ajax({ |
| 167 | + url: this._resourceUrl(), |
| 168 | + dataType: 'json', |
| 169 | + type: 'DELETE' |
| 170 | + }); |
| 171 | + }, |
| 172 | + |
| 173 | + /** |
| 174 | + Is this a new resource? |
| 175 | + */ |
| 176 | + isNew: function() { |
| 177 | + return (this._id() === undefined); |
| 178 | + }, |
| 179 | + |
| 180 | + /** |
| 181 | + @private |
| 182 | +
|
| 183 | + The URL for this resource, based on `resourceUrl` and `id` (which will be |
| 184 | + undefined for new resources). |
| 185 | + */ |
| 186 | + _resourceUrl: function() { |
| 187 | + var url = this.resourceUrl, |
| 188 | + id = this._id(); |
| 189 | + |
| 190 | + if (id !== undefined) |
| 191 | + url += '/' + id; |
| 192 | + |
| 193 | + return url; |
| 194 | + }, |
| 195 | + |
| 196 | + /** |
| 197 | + @private |
| 198 | +
|
| 199 | + The id for this resource. |
| 200 | + */ |
| 201 | + _id: function() { |
| 202 | + return this.get('id'); |
| 203 | + } |
| 204 | +}); |
| 205 | + |
| 206 | +/** |
| 207 | + A controller for RESTful resources |
| 208 | +
|
| 209 | + Extend this class and define the following: |
| 210 | +
|
| 211 | + * `resourceType` -- an Ember.Resource class; the class must have a `serialize()` method |
| 212 | + that returns a JSON representation of the object |
| 213 | + * `resourceUrl` -- (optional) the base url of the resource (e.g. '/contacts/active'); |
| 214 | + will default to the `resourceUrl` for `resourceType` |
| 215 | +*/ |
| 216 | +Ember.ResourceController = Ember.ArrayController.extend({ |
| 217 | + resourceType: Ember.required(), |
| 218 | + |
| 219 | + /** |
| 220 | + @private |
| 221 | + */ |
| 222 | + init: function() { |
| 223 | + this._super(); |
| 224 | + this.clearAll(); |
| 225 | + }, |
| 226 | + |
| 227 | + /** |
| 228 | + Create and load a single `Ember.Resource` from JSON |
| 229 | + */ |
| 230 | + load: function(json) { |
| 231 | + var resource = this.get('resourceType').create().deserialize(json); |
| 232 | + this.pushObject(resource); |
| 233 | + }, |
| 234 | + |
| 235 | + /** |
| 236 | + Create and load `Ember.Resource` objects from a JSON array |
| 237 | + */ |
| 238 | + loadAll: function(json) { |
| 239 | + for (var i=0; i < json.length; i++) |
| 240 | + this.load(json[i]); |
| 241 | + }, |
| 242 | + |
| 243 | + /** |
| 244 | + Clear this controller's contents (without deleting remote resources) |
| 245 | + */ |
| 246 | + clearAll: function() { |
| 247 | + this.set("content", []); |
| 248 | + }, |
| 249 | + |
| 250 | + /** |
| 251 | + Replace this controller's contents with an ajax call to `url` |
| 252 | + */ |
| 253 | + findAll: function() { |
| 254 | + var self = this; |
| 255 | + |
| 256 | + return jQuery.ajax({ |
| 257 | + url: this._resourceUrl(), |
| 258 | + dataType: 'json', |
| 259 | + type: 'GET' |
| 260 | + }).done( function(json) { |
| 261 | + self.clearAll(); |
| 262 | + self.loadAll(json); |
| 263 | + }); |
| 264 | + }, |
| 265 | + |
| 266 | + /** |
| 267 | + @private |
| 268 | +
|
| 269 | + Base URL for ajax calls |
| 270 | +
|
| 271 | + Will use the `resourceUrl` set for this controller, or if that's missing, |
| 272 | + the `resourceUrl` specified for `resourceType`. |
| 273 | + */ |
| 274 | + _resourceUrl: function() { |
| 275 | + if (this.resourceUrl === undefined) |
| 276 | + return this.get('resourceType').prototype.resourceUrl; |
| 277 | + else |
| 278 | + return this.resourceUrl; |
| 279 | + } |
| 280 | +}); |
0 commit comments