Revision d0fe8c12 snf-cyclades-app/synnefo/ui/new_ui/ui/javascripts/ember-data.js
b/snf-cyclades-app/synnefo/ui/new_ui/ui/javascripts/ember-data.js | ||
---|---|---|
1 |
/*! |
|
2 |
* @overview Ember Data |
|
3 |
* @copyright Copyright 2011-2014 Tilde Inc. and contributors. |
|
4 |
* Portions Copyright 2011 LivingSocial Inc. |
|
5 |
* @license Licensed under MIT license (see license.js) |
|
6 |
* @version 1.0.0-beta.5 |
|
7 |
*/ |
|
8 |
|
|
9 |
|
|
10 |
(function() { |
|
11 |
var define, requireModule; |
|
12 |
|
|
13 |
(function() { |
|
14 |
var registry = {}, seen = {}; |
|
15 |
|
|
16 |
define = function(name, deps, callback) { |
|
17 |
registry[name] = { deps: deps, callback: callback }; |
|
18 |
}; |
|
19 |
|
|
20 |
requireModule = function(name) { |
|
21 |
if (seen[name]) { return seen[name]; } |
|
22 |
seen[name] = {}; |
|
23 |
|
|
24 |
var mod, deps, callback, reified , exports; |
|
25 |
|
|
26 |
mod = registry[name]; |
|
27 |
|
|
28 |
if (!mod) { |
|
29 |
throw new Error("Module '" + name + "' not found."); |
|
30 |
} |
|
31 |
|
|
32 |
deps = mod.deps; |
|
33 |
callback = mod.callback; |
|
34 |
reified = []; |
|
35 |
exports; |
|
36 |
|
|
37 |
for (var i=0, l=deps.length; i<l; i++) { |
|
38 |
if (deps[i] === 'exports') { |
|
39 |
reified.push(exports = {}); |
|
40 |
} else { |
|
41 |
reified.push(requireModule(deps[i])); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
var value = callback.apply(this, reified); |
|
46 |
return seen[name] = exports || value; |
|
47 |
}; |
|
48 |
})(); |
|
49 |
(function() { |
|
50 |
/** |
|
51 |
@module ember-data |
|
52 |
*/ |
|
53 |
|
|
54 |
/** |
|
55 |
All Ember Data methods and functions are defined inside of this namespace. |
|
56 |
|
|
57 |
@class DS |
|
58 |
@static |
|
59 |
*/ |
|
60 |
var DS; |
|
61 |
if ('undefined' === typeof DS) { |
|
62 |
/** |
|
63 |
@property VERSION |
|
64 |
@type String |
|
65 |
@default '1.0.0-beta.5' |
|
66 |
@static |
|
67 |
*/ |
|
68 |
DS = Ember.Namespace.create({ |
|
69 |
VERSION: '1.0.0-beta.5' |
|
70 |
}); |
|
71 |
|
|
72 |
if ('undefined' !== typeof window) { |
|
73 |
window.DS = DS; |
|
74 |
} |
|
75 |
|
|
76 |
if (Ember.libraries) { |
|
77 |
Ember.libraries.registerCoreLibrary('Ember Data', DS.VERSION); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
})(); |
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
(function() { |
|
86 |
var get = Ember.get, set = Ember.set, isNone = Ember.isNone; |
|
87 |
|
|
88 |
// Simple dispatcher to support overriding the aliased |
|
89 |
// method in subclasses. |
|
90 |
function aliasMethod(methodName) { |
|
91 |
return function() { |
|
92 |
return this[methodName].apply(this, arguments); |
|
93 |
}; |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
In Ember Data a Serializer is used to serialize and deserialize |
|
98 |
records when they are transfered in and out of an external source. |
|
99 |
This process involves normalizing property names, transforming |
|
100 |
attribute values and serializeing relationships. |
|
101 |
|
|
102 |
For maximum performance Ember Data recomends you use the |
|
103 |
[RESTSerializer](DS.RESTSerializer.html) or one of its subclasses. |
|
104 |
|
|
105 |
`JSONSerializer` is useful for simpler or legacy backends that may |
|
106 |
not support the http://jsonapi.org/ spec. |
|
107 |
|
|
108 |
@class JSONSerializer |
|
109 |
@namespace DS |
|
110 |
*/ |
|
111 |
DS.JSONSerializer = Ember.Object.extend({ |
|
112 |
/** |
|
113 |
The primaryKey is used when serializing and deserializing |
|
114 |
data. Ember Data always uses the `id` propery to store the id of |
|
115 |
the record. The external source may not always follow this |
|
116 |
convention. In these cases it is usesful to override the |
|
117 |
primaryKey property to match the primaryKey of your external |
|
118 |
store. |
|
119 |
|
|
120 |
Example |
|
121 |
|
|
122 |
```javascript |
|
123 |
App.ApplicationSerializer = DS.JSONSerializer.extend({ |
|
124 |
primaryKey: '_id' |
|
125 |
}); |
|
126 |
``` |
|
127 |
|
|
128 |
@property primaryKey |
|
129 |
@type {String} |
|
130 |
@default 'id' |
|
131 |
*/ |
|
132 |
primaryKey: 'id', |
|
133 |
|
|
134 |
/** |
|
135 |
Given a subclass of `DS.Model` and a JSON object this method will |
|
136 |
iterate through each attribute of the `DS.Model` and invoke the |
|
137 |
`DS.Transform#deserialize` method on the matching property of the |
|
138 |
JSON object. This method is typically called after the |
|
139 |
serializer's `normalize` method. |
|
140 |
|
|
141 |
@method applyTransforms |
|
142 |
@private |
|
143 |
@param {subclass of DS.Model} type |
|
144 |
@param {Object} data The data to transform |
|
145 |
@return {Object} data The transformed data object |
|
146 |
*/ |
|
147 |
applyTransforms: function(type, data) { |
|
148 |
type.eachTransformedAttribute(function(key, type) { |
|
149 |
var transform = this.transformFor(type); |
|
150 |
data[key] = transform.deserialize(data[key]); |
|
151 |
}, this); |
|
152 |
|
|
153 |
return data; |
|
154 |
}, |
|
155 |
|
|
156 |
/** |
|
157 |
Normalizes a part of the JSON payload returned by |
|
158 |
the server. You should override this method, munge the hash |
|
159 |
and call super if you have generic normalization to do. |
|
160 |
|
|
161 |
It takes the type of the record that is being normalized |
|
162 |
(as a DS.Model class), the property where the hash was |
|
163 |
originally found, and the hash to normalize. |
|
164 |
|
|
165 |
You can use this method, for example, to normalize underscored keys to camelized |
|
166 |
or other general-purpose normalizations. |
|
167 |
|
|
168 |
Example |
|
169 |
|
|
170 |
```javascript |
|
171 |
App.ApplicationSerializer = DS.JSONSerializer.extend({ |
|
172 |
normalize: function(type, hash) { |
|
173 |
var fields = Ember.get(type, 'fields'); |
|
174 |
fields.forEach(function(field) { |
|
175 |
var payloadField = Ember.String.underscore(field); |
|
176 |
if (field === payloadField) { return; } |
|
177 |
|
|
178 |
hash[field] = hash[payloadField]; |
|
179 |
delete hash[payloadField]; |
|
180 |
}); |
|
181 |
return this._super.apply(this, arguments); |
|
182 |
} |
|
183 |
}); |
|
184 |
``` |
|
185 |
|
|
186 |
@method normalize |
|
187 |
@param {subclass of DS.Model} type |
|
188 |
@param {Object} hash |
|
189 |
@return {Object} |
|
190 |
*/ |
|
191 |
normalize: function(type, hash) { |
|
192 |
if (!hash) { return hash; } |
|
193 |
|
|
194 |
this.applyTransforms(type, hash); |
|
195 |
return hash; |
|
196 |
}, |
|
197 |
|
|
198 |
// SERIALIZE |
|
199 |
/** |
|
200 |
Called when a record is saved in order to convert the |
|
201 |
record into JSON. |
|
202 |
|
|
203 |
By default, it creates a JSON object with a key for |
|
204 |
each attribute and belongsTo relationship. |
|
205 |
|
|
206 |
For example, consider this model: |
|
207 |
|
|
208 |
```javascript |
|
209 |
App.Comment = DS.Model.extend({ |
|
210 |
title: DS.attr(), |
|
211 |
body: DS.attr(), |
|
212 |
|
|
213 |
author: DS.belongsTo('user') |
|
214 |
}); |
|
215 |
``` |
|
216 |
|
|
217 |
The default serialization would create a JSON object like: |
|
218 |
|
|
219 |
```javascript |
|
220 |
{ |
|
221 |
"title": "Rails is unagi", |
|
222 |
"body": "Rails? Omakase? O_O", |
|
223 |
"author": 12 |
|
224 |
} |
|
225 |
``` |
|
226 |
|
|
227 |
By default, attributes are passed through as-is, unless |
|
228 |
you specified an attribute type (`DS.attr('date')`). If |
|
229 |
you specify a transform, the JavaScript value will be |
|
230 |
serialized when inserted into the JSON hash. |
|
231 |
|
|
232 |
By default, belongs-to relationships are converted into |
|
233 |
IDs when inserted into the JSON hash. |
|
234 |
|
|
235 |
## IDs |
|
236 |
|
|
237 |
`serialize` takes an options hash with a single option: |
|
238 |
`includeId`. If this option is `true`, `serialize` will, |
|
239 |
by default include the ID in the JSON object it builds. |
|
240 |
|
|
241 |
The adapter passes in `includeId: true` when serializing |
|
242 |
a record for `createRecord`, but not for `updateRecord`. |
|
243 |
|
|
244 |
## Customization |
|
245 |
|
|
246 |
Your server may expect a different JSON format than the |
|
247 |
built-in serialization format. |
|
248 |
|
|
249 |
In that case, you can implement `serialize` yourself and |
|
250 |
return a JSON hash of your choosing. |
|
251 |
|
|
252 |
```javascript |
|
253 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
254 |
serialize: function(post, options) { |
|
255 |
var json = { |
|
256 |
POST_TTL: post.get('title'), |
|
257 |
POST_BDY: post.get('body'), |
|
258 |
POST_CMS: post.get('comments').mapProperty('id') |
|
259 |
} |
|
260 |
|
|
261 |
if (options.includeId) { |
|
262 |
json.POST_ID_ = post.get('id'); |
|
263 |
} |
|
264 |
|
|
265 |
return json; |
|
266 |
} |
|
267 |
}); |
|
268 |
``` |
|
269 |
|
|
270 |
## Customizing an App-Wide Serializer |
|
271 |
|
|
272 |
If you want to define a serializer for your entire |
|
273 |
application, you'll probably want to use `eachAttribute` |
|
274 |
and `eachRelationship` on the record. |
|
275 |
|
|
276 |
```javascript |
|
277 |
App.ApplicationSerializer = DS.JSONSerializer.extend({ |
|
278 |
serialize: function(record, options) { |
|
279 |
var json = {}; |
|
280 |
|
|
281 |
record.eachAttribute(function(name) { |
|
282 |
json[serverAttributeName(name)] = record.get(name); |
|
283 |
}) |
|
284 |
|
|
285 |
record.eachRelationship(function(name, relationship) { |
|
286 |
if (relationship.kind === 'hasMany') { |
|
287 |
json[serverHasManyName(name)] = record.get(name).mapBy('id'); |
|
288 |
} |
|
289 |
}); |
|
290 |
|
|
291 |
if (options.includeId) { |
|
292 |
json.ID_ = record.get('id'); |
|
293 |
} |
|
294 |
|
|
295 |
return json; |
|
296 |
} |
|
297 |
}); |
|
298 |
|
|
299 |
function serverAttributeName(attribute) { |
|
300 |
return attribute.underscore().toUpperCase(); |
|
301 |
} |
|
302 |
|
|
303 |
function serverHasManyName(name) { |
|
304 |
return serverAttributeName(name.singularize()) + "_IDS"; |
|
305 |
} |
|
306 |
``` |
|
307 |
|
|
308 |
This serializer will generate JSON that looks like this: |
|
309 |
|
|
310 |
```javascript |
|
311 |
{ |
|
312 |
"TITLE": "Rails is omakase", |
|
313 |
"BODY": "Yep. Omakase.", |
|
314 |
"COMMENT_IDS": [ 1, 2, 3 ] |
|
315 |
} |
|
316 |
``` |
|
317 |
|
|
318 |
## Tweaking the Default JSON |
|
319 |
|
|
320 |
If you just want to do some small tweaks on the default JSON, |
|
321 |
you can call super first and make the tweaks on the returned |
|
322 |
JSON. |
|
323 |
|
|
324 |
```javascript |
|
325 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
326 |
serialize: function(record, options) { |
|
327 |
var json = this._super.apply(this, arguments); |
|
328 |
|
|
329 |
json.subject = json.title; |
|
330 |
delete json.title; |
|
331 |
|
|
332 |
return json; |
|
333 |
} |
|
334 |
}); |
|
335 |
``` |
|
336 |
|
|
337 |
@method serialize |
|
338 |
@param {subclass of DS.Model} record |
|
339 |
@param {Object} options |
|
340 |
@return {Object} json |
|
341 |
*/ |
|
342 |
serialize: function(record, options) { |
|
343 |
var json = {}; |
|
344 |
|
|
345 |
if (options && options.includeId) { |
|
346 |
var id = get(record, 'id'); |
|
347 |
|
|
348 |
if (id) { |
|
349 |
json[get(this, 'primaryKey')] = id; |
|
350 |
} |
|
351 |
} |
|
352 |
|
|
353 |
record.eachAttribute(function(key, attribute) { |
|
354 |
this.serializeAttribute(record, json, key, attribute); |
|
355 |
}, this); |
|
356 |
|
|
357 |
record.eachRelationship(function(key, relationship) { |
|
358 |
if (relationship.kind === 'belongsTo') { |
|
359 |
this.serializeBelongsTo(record, json, relationship); |
|
360 |
} else if (relationship.kind === 'hasMany') { |
|
361 |
this.serializeHasMany(record, json, relationship); |
|
362 |
} |
|
363 |
}, this); |
|
364 |
|
|
365 |
return json; |
|
366 |
}, |
|
367 |
|
|
368 |
/** |
|
369 |
`serializeAttribute` can be used to customize how `DS.attr` |
|
370 |
properties are serialized |
|
371 |
|
|
372 |
For example if you wanted to ensure all you attributes were always |
|
373 |
serialized as properties on an `attributes` object you could |
|
374 |
write: |
|
375 |
|
|
376 |
```javascript |
|
377 |
App.ApplicationSerializer = DS.JSONSerializer.extend({ |
|
378 |
serializeAttribute: function(record, json, key, attributes) { |
|
379 |
json.attributes = json.attributes || {}; |
|
380 |
this._super(record, json.attributes, key, attributes); |
|
381 |
} |
|
382 |
}); |
|
383 |
``` |
|
384 |
|
|
385 |
@method serializeAttribute |
|
386 |
@param {DS.Model} record |
|
387 |
@param {Object} json |
|
388 |
@param {String} key |
|
389 |
@param {Object} attribute |
|
390 |
*/ |
|
391 |
serializeAttribute: function(record, json, key, attribute) { |
|
392 |
var attrs = get(this, 'attrs'); |
|
393 |
var value = get(record, key), type = attribute.type; |
|
394 |
|
|
395 |
if (type) { |
|
396 |
var transform = this.transformFor(type); |
|
397 |
value = transform.serialize(value); |
|
398 |
} |
|
399 |
|
|
400 |
// if provided, use the mapping provided by `attrs` in |
|
401 |
// the serializer |
|
402 |
key = attrs && attrs[key] || (this.keyForAttribute ? this.keyForAttribute(key) : key); |
|
403 |
|
|
404 |
json[key] = value; |
|
405 |
}, |
|
406 |
|
|
407 |
/** |
|
408 |
`serializeBelongsTo` can be used to customize how `DS.belongsTo` |
|
409 |
properties are serialized. |
|
410 |
|
|
411 |
Example |
|
412 |
|
|
413 |
```javascript |
|
414 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
415 |
serializeBelongsTo: function(record, json, relationship) { |
|
416 |
var key = relationship.key; |
|
417 |
|
|
418 |
var belongsTo = get(record, key); |
|
419 |
|
|
420 |
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key; |
|
421 |
|
|
422 |
json[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.toJSON(); |
|
423 |
} |
|
424 |
}); |
|
425 |
``` |
|
426 |
|
|
427 |
@method serializeBelongsTo |
|
428 |
@param {DS.Model} record |
|
429 |
@param {Object} json |
|
430 |
@param {Object} relationship |
|
431 |
*/ |
|
432 |
serializeBelongsTo: function(record, json, relationship) { |
|
433 |
var key = relationship.key; |
|
434 |
|
|
435 |
var belongsTo = get(record, key); |
|
436 |
|
|
437 |
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key; |
|
438 |
|
|
439 |
if (isNone(belongsTo)) { |
|
440 |
json[key] = belongsTo; |
|
441 |
} else { |
|
442 |
json[key] = get(belongsTo, 'id'); |
|
443 |
} |
|
444 |
|
|
445 |
if (relationship.options.polymorphic) { |
|
446 |
this.serializePolymorphicType(record, json, relationship); |
|
447 |
} |
|
448 |
}, |
|
449 |
|
|
450 |
/** |
|
451 |
`serializeHasMany` can be used to customize how `DS.hasMany` |
|
452 |
properties are serialized. |
|
453 |
|
|
454 |
Example |
|
455 |
|
|
456 |
```javascript |
|
457 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
458 |
serializeHasMany: function(record, json, relationship) { |
|
459 |
var key = relationship.key; |
|
460 |
if (key === 'comments') { |
|
461 |
return; |
|
462 |
} else { |
|
463 |
this._super.apply(this, arguments); |
|
464 |
} |
|
465 |
} |
|
466 |
}); |
|
467 |
``` |
|
468 |
|
|
469 |
@method serializeHasMany |
|
470 |
@param {DS.Model} record |
|
471 |
@param {Object} json |
|
472 |
@param {Object} relationship |
|
473 |
*/ |
|
474 |
serializeHasMany: function(record, json, relationship) { |
|
475 |
var key = relationship.key; |
|
476 |
|
|
477 |
var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship); |
|
478 |
|
|
479 |
if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany') { |
|
480 |
json[key] = get(record, key).mapBy('id'); |
|
481 |
// TODO support for polymorphic manyToNone and manyToMany relationships |
|
482 |
} |
|
483 |
}, |
|
484 |
|
|
485 |
/** |
|
486 |
You can use this method to customize how polymorphic objects are |
|
487 |
serialized. Objects are considered to be polymorphic if |
|
488 |
`{polymorphic: true}` is pass as the second argument to the |
|
489 |
`DS.belongsTo` function. |
|
490 |
|
|
491 |
Example |
|
492 |
|
|
493 |
```javascript |
|
494 |
App.CommentSerializer = DS.JSONSerializer.extend({ |
|
495 |
serializePolymorphicType: function(record, json, relationship) { |
|
496 |
var key = relationship.key, |
|
497 |
belongsTo = get(record, key); |
|
498 |
key = this.keyForAttribute ? this.keyForAttribute(key) : key; |
|
499 |
json[key + "_type"] = belongsTo.constructor.typeKey; |
|
500 |
} |
|
501 |
}); |
|
502 |
``` |
|
503 |
|
|
504 |
@method serializePolymorphicType |
|
505 |
@param {DS.Model} record |
|
506 |
@param {Object} json |
|
507 |
@param {Object} relationship |
|
508 |
*/ |
|
509 |
serializePolymorphicType: Ember.K, |
|
510 |
|
|
511 |
// EXTRACT |
|
512 |
|
|
513 |
/** |
|
514 |
The `extract` method is used to deserialize payload data from the |
|
515 |
server. By default the `JSONSerializer` does not push the records |
|
516 |
into the store. However records that subclass `JSONSerializer` |
|
517 |
such as the `RESTSerializer` may push records into the store as |
|
518 |
part of the extract call. |
|
519 |
|
|
520 |
This method deletegates to a more specific extract method based on |
|
521 |
the `requestType`. |
|
522 |
|
|
523 |
Example |
|
524 |
|
|
525 |
```javascript |
|
526 |
var get = Ember.get; |
|
527 |
socket.on('message', function(message) { |
|
528 |
var modelName = message.model; |
|
529 |
var data = message.data; |
|
530 |
var type = store.modelFor(modelName); |
|
531 |
var serializer = store.serializerFor(type.typeKey); |
|
532 |
var record = serializer.extract(store, type, data, get(data, 'id'), 'single'); |
|
533 |
store.push(modelName, record); |
|
534 |
}); |
|
535 |
``` |
|
536 |
|
|
537 |
@method extract |
|
538 |
@param {DS.Store} store |
|
539 |
@param {subclass of DS.Model} type |
|
540 |
@param {Object} payload |
|
541 |
@param {String or Number} id |
|
542 |
@param {String} requestType |
|
543 |
@return {Object} json The deserialized payload |
|
544 |
*/ |
|
545 |
extract: function(store, type, payload, id, requestType) { |
|
546 |
this.extractMeta(store, type, payload); |
|
547 |
|
|
548 |
var specificExtract = "extract" + requestType.charAt(0).toUpperCase() + requestType.substr(1); |
|
549 |
return this[specificExtract](store, type, payload, id, requestType); |
|
550 |
}, |
|
551 |
|
|
552 |
/** |
|
553 |
`extractFindAll` is a hook into the extract method used when a |
|
554 |
call is made to `DS.Store#findAll`. By default this method is an |
|
555 |
alias for [extractArray](#method_extractArray). |
|
556 |
|
|
557 |
@method extractFindAll |
|
558 |
@param {DS.Store} store |
|
559 |
@param {subclass of DS.Model} type |
|
560 |
@param {Object} payload |
|
561 |
@return {Array} array An array of deserialized objects |
|
562 |
*/ |
|
563 |
extractFindAll: aliasMethod('extractArray'), |
|
564 |
/** |
|
565 |
`extractFindQuery` is a hook into the extract method used when a |
|
566 |
call is made to `DS.Store#findQuery`. By default this method is an |
|
567 |
alias for [extractArray](#method_extractArray). |
|
568 |
|
|
569 |
@method extractFindQuery |
|
570 |
@param {DS.Store} store |
|
571 |
@param {subclass of DS.Model} type |
|
572 |
@param {Object} payload |
|
573 |
@return {Array} array An array of deserialized objects |
|
574 |
*/ |
|
575 |
extractFindQuery: aliasMethod('extractArray'), |
|
576 |
/** |
|
577 |
`extractFindMany` is a hook into the extract method used when a |
|
578 |
call is made to `DS.Store#findMany`. By default this method is |
|
579 |
alias for [extractArray](#method_extractArray). |
|
580 |
|
|
581 |
@method extractFindMany |
|
582 |
@param {DS.Store} store |
|
583 |
@param {subclass of DS.Model} type |
|
584 |
@param {Object} payload |
|
585 |
@return {Array} array An array of deserialized objects |
|
586 |
*/ |
|
587 |
extractFindMany: aliasMethod('extractArray'), |
|
588 |
/** |
|
589 |
`extractFindHasMany` is a hook into the extract method used when a |
|
590 |
call is made to `DS.Store#findHasMany`. By default this method is |
|
591 |
alias for [extractArray](#method_extractArray). |
|
592 |
|
|
593 |
@method extractFindHasMany |
|
594 |
@param {DS.Store} store |
|
595 |
@param {subclass of DS.Model} type |
|
596 |
@param {Object} payload |
|
597 |
@return {Array} array An array of deserialized objects |
|
598 |
*/ |
|
599 |
extractFindHasMany: aliasMethod('extractArray'), |
|
600 |
|
|
601 |
/** |
|
602 |
`extractCreateRecord` is a hook into the extract method used when a |
|
603 |
call is made to `DS.Store#createRecord`. By default this method is |
|
604 |
alias for [extractSave](#method_extractSave). |
|
605 |
|
|
606 |
@method extractCreateRecord |
|
607 |
@param {DS.Store} store |
|
608 |
@param {subclass of DS.Model} type |
|
609 |
@param {Object} payload |
|
610 |
@return {Object} json The deserialized payload |
|
611 |
*/ |
|
612 |
extractCreateRecord: aliasMethod('extractSave'), |
|
613 |
/** |
|
614 |
`extractUpdateRecord` is a hook into the extract method used when |
|
615 |
a call is made to `DS.Store#update`. By default this method is alias |
|
616 |
for [extractSave](#method_extractSave). |
|
617 |
|
|
618 |
@method extractUpdateRecord |
|
619 |
@param {DS.Store} store |
|
620 |
@param {subclass of DS.Model} type |
|
621 |
@param {Object} payload |
|
622 |
@return {Object} json The deserialized payload |
|
623 |
*/ |
|
624 |
extractUpdateRecord: aliasMethod('extractSave'), |
|
625 |
/** |
|
626 |
`extractDeleteRecord` is a hook into the extract method used when |
|
627 |
a call is made to `DS.Store#deleteRecord`. By default this method is |
|
628 |
alias for [extractSave](#method_extractSave). |
|
629 |
|
|
630 |
@method extractDeleteRecord |
|
631 |
@param {DS.Store} store |
|
632 |
@param {subclass of DS.Model} type |
|
633 |
@param {Object} payload |
|
634 |
@return {Object} json The deserialized payload |
|
635 |
*/ |
|
636 |
extractDeleteRecord: aliasMethod('extractSave'), |
|
637 |
|
|
638 |
/** |
|
639 |
`extractFind` is a hook into the extract method used when |
|
640 |
a call is made to `DS.Store#find`. By default this method is |
|
641 |
alias for [extractSingle](#method_extractSingle). |
|
642 |
|
|
643 |
@method extractFind |
|
644 |
@param {DS.Store} store |
|
645 |
@param {subclass of DS.Model} type |
|
646 |
@param {Object} payload |
|
647 |
@return {Object} json The deserialized payload |
|
648 |
*/ |
|
649 |
extractFind: aliasMethod('extractSingle'), |
|
650 |
/** |
|
651 |
`extractFindBelongsTo` is a hook into the extract method used when |
|
652 |
a call is made to `DS.Store#findBelongsTo`. By default this method is |
|
653 |
alias for [extractSingle](#method_extractSingle). |
|
654 |
|
|
655 |
@method extractFindBelongsTo |
|
656 |
@param {DS.Store} store |
|
657 |
@param {subclass of DS.Model} type |
|
658 |
@param {Object} payload |
|
659 |
@return {Object} json The deserialized payload |
|
660 |
*/ |
|
661 |
extractFindBelongsTo: aliasMethod('extractSingle'), |
|
662 |
/** |
|
663 |
`extractSave` is a hook into the extract method used when a call |
|
664 |
is made to `DS.Model#save`. By default this method is alias |
|
665 |
for [extractSingle](#method_extractSingle). |
|
666 |
|
|
667 |
@method extractSave |
|
668 |
@param {DS.Store} store |
|
669 |
@param {subclass of DS.Model} type |
|
670 |
@param {Object} payload |
|
671 |
@return {Object} json The deserialized payload |
|
672 |
*/ |
|
673 |
extractSave: aliasMethod('extractSingle'), |
|
674 |
|
|
675 |
/** |
|
676 |
`extractSingle` is used to deserialize a single record returned |
|
677 |
from the adapter. |
|
678 |
|
|
679 |
Example |
|
680 |
|
|
681 |
```javascript |
|
682 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
683 |
extractSingle: function(store, type, payload) { |
|
684 |
payload.comments = payload._embedded.comment; |
|
685 |
delete payload._embedded; |
|
686 |
|
|
687 |
return this._super(store, type, payload); |
|
688 |
}, |
|
689 |
}); |
|
690 |
``` |
|
691 |
|
|
692 |
@method extractSingle |
|
693 |
@param {DS.Store} store |
|
694 |
@param {subclass of DS.Model} type |
|
695 |
@param {Object} payload |
|
696 |
@return {Object} json The deserialized payload |
|
697 |
*/ |
|
698 |
extractSingle: function(store, type, payload) { |
|
699 |
return this.normalize(type, payload); |
|
700 |
}, |
|
701 |
|
|
702 |
/** |
|
703 |
`extractArray` is used to deserialize an array of records |
|
704 |
returned from the adapter. |
|
705 |
|
|
706 |
Example |
|
707 |
|
|
708 |
```javascript |
|
709 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
710 |
extractArray: function(store, type, payload) { |
|
711 |
return payload.map(function(json) { |
|
712 |
return this.extractSingle(json); |
|
713 |
}, this); |
|
714 |
} |
|
715 |
}); |
|
716 |
``` |
|
717 |
|
|
718 |
@method extractArray |
|
719 |
@param {DS.Store} store |
|
720 |
@param {subclass of DS.Model} type |
|
721 |
@param {Object} payload |
|
722 |
@return {Array} array An array of deserialized objects |
|
723 |
*/ |
|
724 |
extractArray: function(store, type, payload) { |
|
725 |
return this.normalize(type, payload); |
|
726 |
}, |
|
727 |
|
|
728 |
/** |
|
729 |
`extractMeta` is used to deserialize any meta information in the |
|
730 |
adapter payload. By default Ember Data expects meta information to |
|
731 |
be located on the `meta` property of the payload object. |
|
732 |
|
|
733 |
Example |
|
734 |
|
|
735 |
```javascript |
|
736 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
737 |
extractMeta: function(store, type, payload) { |
|
738 |
if (payload && payload._pagination) { |
|
739 |
store.metaForType(type, payload._pagination); |
|
740 |
delete payload._pagination; |
|
741 |
} |
|
742 |
} |
|
743 |
}); |
|
744 |
``` |
|
745 |
|
|
746 |
@method extractMeta |
|
747 |
@param {DS.Store} store |
|
748 |
@param {subclass of DS.Model} type |
|
749 |
@param {Object} payload |
|
750 |
*/ |
|
751 |
extractMeta: function(store, type, payload) { |
|
752 |
if (payload && payload.meta) { |
|
753 |
store.metaForType(type, payload.meta); |
|
754 |
delete payload.meta; |
|
755 |
} |
|
756 |
}, |
|
757 |
|
|
758 |
/** |
|
759 |
`keyForAttribute` can be used to define rules for how to convert an |
|
760 |
attribute name in your model to a key in your JSON. |
|
761 |
|
|
762 |
Example |
|
763 |
|
|
764 |
```javascript |
|
765 |
App.ApplicationSerializer = DS.RESTSerializer.extend({ |
|
766 |
keyForAttribute: function(attr) { |
|
767 |
return Ember.String.underscore(attr).toUpperCase(); |
|
768 |
} |
|
769 |
}); |
|
770 |
``` |
|
771 |
|
|
772 |
@method keyForAttribute |
|
773 |
@param {String} key |
|
774 |
@return {String} normalized key |
|
775 |
*/ |
|
776 |
|
|
777 |
|
|
778 |
/** |
|
779 |
`keyForRelationship` can be used to define a custom key when |
|
780 |
serializeing relationship properties. By default `JSONSerializer` |
|
781 |
does not provide an implementation of this method. |
|
782 |
|
|
783 |
Example |
|
784 |
|
|
785 |
```javascript |
|
786 |
App.PostSerializer = DS.JSONSerializer.extend({ |
|
787 |
keyForRelationship: function(key, relationship) { |
|
788 |
return 'rel_' + Ember.String.underscore(key); |
|
789 |
} |
|
790 |
}); |
|
791 |
``` |
|
792 |
|
|
793 |
@method keyForRelationship |
|
794 |
@param {String} key |
|
795 |
@param {String} relationship type |
|
796 |
@return {String} normalized key |
|
797 |
*/ |
|
798 |
|
|
799 |
// HELPERS |
|
800 |
|
|
801 |
/** |
|
802 |
@method transformFor |
|
803 |
@private |
|
804 |
@param {String} attributeType |
|
805 |
@param {Boolean} skipAssertion |
|
806 |
@return {DS.Transform} transform |
|
807 |
*/ |
|
808 |
transformFor: function(attributeType, skipAssertion) { |
|
809 |
var transform = this.container.lookup('transform:' + attributeType); |
|
810 |
Ember.assert("Unable to find transform for '" + attributeType + "'", skipAssertion || !!transform); |
|
811 |
return transform; |
|
812 |
} |
|
813 |
}); |
|
814 |
|
|
815 |
})(); |
|
816 |
|
|
817 |
|
|
818 |
|
|
819 |
(function() { |
|
820 |
/** |
|
821 |
@module ember-data |
|
822 |
*/ |
|
823 |
var get = Ember.get, capitalize = Ember.String.capitalize, underscore = Ember.String.underscore, DS = window.DS ; |
|
824 |
|
|
825 |
/** |
|
826 |
Extend `Ember.DataAdapter` with ED specific code. |
|
827 |
|
|
828 |
@class DebugAdapter |
|
829 |
@namespace DS |
|
830 |
@extends Ember.DataAdapter |
|
831 |
@private |
|
832 |
*/ |
|
833 |
DS.DebugAdapter = Ember.DataAdapter.extend({ |
|
834 |
getFilters: function() { |
|
835 |
return [ |
|
836 |
{ name: 'isNew', desc: 'New' }, |
|
837 |
{ name: 'isModified', desc: 'Modified' }, |
|
838 |
{ name: 'isClean', desc: 'Clean' } |
|
839 |
]; |
|
840 |
}, |
|
841 |
|
|
842 |
detect: function(klass) { |
|
843 |
return klass !== DS.Model && DS.Model.detect(klass); |
|
844 |
}, |
|
845 |
|
|
846 |
columnsForType: function(type) { |
|
847 |
var columns = [{ name: 'id', desc: 'Id' }], count = 0, self = this; |
|
848 |
get(type, 'attributes').forEach(function(name, meta) { |
|
849 |
if (count++ > self.attributeLimit) { return false; } |
|
850 |
var desc = capitalize(underscore(name).replace('_', ' ')); |
|
851 |
columns.push({ name: name, desc: desc }); |
|
852 |
}); |
|
853 |
return columns; |
|
854 |
}, |
|
855 |
|
|
856 |
getRecords: function(type) { |
|
857 |
return this.get('store').all(type); |
|
858 |
}, |
|
859 |
|
|
860 |
getRecordColumnValues: function(record) { |
|
861 |
var self = this, count = 0, |
|
862 |
columnValues = { id: get(record, 'id') }; |
|
863 |
|
|
864 |
record.eachAttribute(function(key) { |
|
865 |
if (count++ > self.attributeLimit) { |
|
866 |
return false; |
|
867 |
} |
|
868 |
var value = get(record, key); |
|
869 |
columnValues[key] = value; |
|
870 |
}); |
|
871 |
return columnValues; |
|
872 |
}, |
|
873 |
|
|
874 |
getRecordKeywords: function(record) { |
|
875 |
var keywords = [], keys = Ember.A(['id']); |
|
876 |
record.eachAttribute(function(key) { |
|
877 |
keys.push(key); |
|
878 |
}); |
|
879 |
keys.forEach(function(key) { |
|
880 |
keywords.push(get(record, key)); |
|
881 |
}); |
|
882 |
return keywords; |
|
883 |
}, |
|
884 |
|
|
885 |
getRecordFilterValues: function(record) { |
|
886 |
return { |
|
887 |
isNew: record.get('isNew'), |
|
888 |
isModified: record.get('isDirty') && !record.get('isNew'), |
|
889 |
isClean: !record.get('isDirty') |
|
890 |
}; |
|
891 |
}, |
|
892 |
|
|
893 |
getRecordColor: function(record) { |
|
894 |
var color = 'black'; |
|
895 |
if (record.get('isNew')) { |
|
896 |
color = 'green'; |
|
897 |
} else if (record.get('isDirty')) { |
|
898 |
color = 'blue'; |
|
899 |
} |
|
900 |
return color; |
|
901 |
}, |
|
902 |
|
|
903 |
observeRecord: function(record, recordUpdated) { |
|
904 |
var releaseMethods = Ember.A(), self = this, |
|
905 |
keysToObserve = Ember.A(['id', 'isNew', 'isDirty']); |
|
906 |
|
|
907 |
record.eachAttribute(function(key) { |
|
908 |
keysToObserve.push(key); |
|
909 |
}); |
|
910 |
|
|
911 |
keysToObserve.forEach(function(key) { |
|
912 |
var handler = function() { |
|
913 |
recordUpdated(self.wrapRecord(record)); |
|
914 |
}; |
|
915 |
Ember.addObserver(record, key, handler); |
|
916 |
releaseMethods.push(function() { |
|
917 |
Ember.removeObserver(record, key, handler); |
|
918 |
}); |
|
919 |
}); |
|
920 |
|
|
921 |
var release = function() { |
|
922 |
releaseMethods.forEach(function(fn) { fn(); } ); |
|
923 |
}; |
|
924 |
|
|
925 |
return release; |
|
926 |
} |
|
927 |
|
|
928 |
}); |
|
929 |
|
|
930 |
})(); |
|
931 |
|
|
932 |
|
|
933 |
|
|
934 |
(function() { |
|
935 |
/** |
|
936 |
The `DS.Transform` class is used to serialize and deserialize model |
|
937 |
attributes when they are saved or loaded from an |
|
938 |
adapter. Subclassing `DS.Transform` is useful for creating custom |
|
939 |
attributes. All subclasses of `DS.Transform` must implement a |
|
940 |
`serialize` and a `deserialize` method. |
|
941 |
|
|
942 |
Example |
|
943 |
|
|
944 |
```javascript |
|
945 |
App.RawTransform = DS.Transform.extend({ |
|
946 |
deserialize: function(serialized) { |
|
947 |
return serialized; |
|
948 |
}, |
|
949 |
serialize: function(deserialized) { |
|
950 |
return deserialized; |
|
951 |
} |
|
952 |
}); |
|
953 |
``` |
|
954 |
|
|
955 |
Usage |
|
956 |
|
|
957 |
```javascript |
|
958 |
var attr = DS.attr; |
|
959 |
App.Requirement = DS.Model.extend({ |
|
960 |
name: attr('string'), |
|
961 |
optionsArray: attr('raw') |
|
962 |
}); |
|
963 |
``` |
|
964 |
|
|
965 |
@class Transform |
|
966 |
@namespace DS |
|
967 |
*/ |
|
968 |
DS.Transform = Ember.Object.extend({ |
|
969 |
/** |
|
970 |
When given a deserialized value from a record attribute this |
|
971 |
method must return the serialized value. |
|
972 |
|
|
973 |
Example |
|
974 |
|
|
975 |
```javascript |
|
976 |
serialize: function(deserialized) { |
|
977 |
return Ember.isEmpty(deserialized) ? null : Number(deserialized); |
|
978 |
} |
|
979 |
``` |
|
980 |
|
|
981 |
@method serialize |
|
982 |
@param deserialized The deserialized value |
|
983 |
@return The serialized value |
|
984 |
*/ |
|
985 |
serialize: Ember.required(), |
|
986 |
|
|
987 |
/** |
|
988 |
When given a serialize value from a JSON object this method must |
|
989 |
return the deserialized value for the record attribute. |
|
990 |
|
|
991 |
Example |
|
992 |
|
|
993 |
```javascript |
|
994 |
deserialize: function(serialized) { |
|
995 |
return empty(serialized) ? null : Number(serialized); |
|
996 |
} |
|
997 |
``` |
|
998 |
|
|
999 |
@method deserialize |
|
1000 |
@param serialized The serialized value |
|
1001 |
@return The deserialized value |
|
1002 |
*/ |
|
1003 |
deserialize: Ember.required() |
|
1004 |
|
|
1005 |
}); |
|
1006 |
|
|
1007 |
})(); |
|
1008 |
|
|
1009 |
|
|
1010 |
|
|
1011 |
(function() { |
|
1012 |
|
|
1013 |
/** |
|
1014 |
The `DS.BooleanTransform` class is used to serialize and deserialize |
|
1015 |
boolean attributes on Ember Data record objects. This transform is |
|
1016 |
used when `boolean` is passed as the type parameter to the |
|
1017 |
[DS.attr](../../data#method_attr) function. |
|
1018 |
|
|
1019 |
Usage |
|
1020 |
|
|
1021 |
```javascript |
|
1022 |
var attr = DS.attr; |
|
1023 |
App.User = DS.Model.extend({ |
|
1024 |
isAdmin: attr('boolean'), |
|
1025 |
name: attr('string'), |
|
1026 |
email: attr('string') |
|
1027 |
}); |
|
1028 |
``` |
|
1029 |
|
|
1030 |
@class BooleanTransform |
|
1031 |
@extends DS.Transform |
|
1032 |
@namespace DS |
|
1033 |
*/ |
|
1034 |
DS.BooleanTransform = DS.Transform.extend({ |
|
1035 |
deserialize: function(serialized) { |
|
1036 |
var type = typeof serialized; |
|
1037 |
|
|
1038 |
if (type === "boolean") { |
|
1039 |
return serialized; |
|
1040 |
} else if (type === "string") { |
|
1041 |
return serialized.match(/^true$|^t$|^1$/i) !== null; |
|
1042 |
} else if (type === "number") { |
|
1043 |
return serialized === 1; |
|
1044 |
} else { |
|
1045 |
return false; |
|
1046 |
} |
|
1047 |
}, |
|
1048 |
|
|
1049 |
serialize: function(deserialized) { |
|
1050 |
return Boolean(deserialized); |
|
1051 |
} |
|
1052 |
}); |
|
1053 |
|
|
1054 |
})(); |
|
1055 |
|
|
1056 |
|
|
1057 |
|
|
1058 |
(function() { |
|
1059 |
/** |
|
1060 |
The `DS.DateTransform` class is used to serialize and deserialize |
|
1061 |
date attributes on Ember Data record objects. This transform is used |
|
1062 |
when `date` is passed as the type parameter to the |
|
1063 |
[DS.attr](../../data#method_attr) function. |
|
1064 |
|
|
1065 |
```javascript |
|
1066 |
var attr = DS.attr; |
|
1067 |
App.Score = DS.Model.extend({ |
|
1068 |
value: attr('number'), |
|
1069 |
player: DS.belongsTo('player'), |
|
1070 |
date: attr('date') |
|
1071 |
}); |
|
1072 |
``` |
|
1073 |
|
|
1074 |
@class DateTransform |
|
1075 |
@extends DS.Transform |
|
1076 |
@namespace DS |
|
1077 |
*/ |
|
1078 |
DS.DateTransform = DS.Transform.extend({ |
|
1079 |
|
|
1080 |
deserialize: function(serialized) { |
|
1081 |
var type = typeof serialized; |
|
1082 |
|
|
1083 |
if (type === "string") { |
|
1084 |
return new Date(Ember.Date.parse(serialized)); |
|
1085 |
} else if (type === "number") { |
|
1086 |
return new Date(serialized); |
|
1087 |
} else if (serialized === null || serialized === undefined) { |
|
1088 |
// if the value is not present in the data, |
|
1089 |
// return undefined, not null. |
|
1090 |
return serialized; |
|
1091 |
} else { |
|
1092 |
return null; |
|
1093 |
} |
|
1094 |
}, |
|
1095 |
|
|
1096 |
serialize: function(date) { |
|
1097 |
if (date instanceof Date) { |
|
1098 |
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; |
|
1099 |
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; |
|
1100 |
|
|
1101 |
var pad = function(num) { |
|
1102 |
return num < 10 ? "0"+num : ""+num; |
|
1103 |
}; |
|
1104 |
|
|
1105 |
var utcYear = date.getUTCFullYear(), |
|
1106 |
utcMonth = date.getUTCMonth(), |
|
1107 |
utcDayOfMonth = date.getUTCDate(), |
|
1108 |
utcDay = date.getUTCDay(), |
|
1109 |
utcHours = date.getUTCHours(), |
|
1110 |
utcMinutes = date.getUTCMinutes(), |
|
1111 |
utcSeconds = date.getUTCSeconds(); |
|
1112 |
|
|
1113 |
|
|
1114 |
var dayOfWeek = days[utcDay]; |
|
1115 |
var dayOfMonth = pad(utcDayOfMonth); |
|
1116 |
var month = months[utcMonth]; |
|
1117 |
|
|
1118 |
return dayOfWeek + ", " + dayOfMonth + " " + month + " " + utcYear + " " + |
|
1119 |
pad(utcHours) + ":" + pad(utcMinutes) + ":" + pad(utcSeconds) + " GMT"; |
|
1120 |
} else { |
|
1121 |
return null; |
|
1122 |
} |
|
1123 |
} |
|
1124 |
|
|
1125 |
}); |
|
1126 |
|
|
1127 |
})(); |
|
1128 |
|
|
1129 |
|
|
1130 |
|
|
1131 |
(function() { |
|
1132 |
var empty = Ember.isEmpty; |
|
1133 |
/** |
|
1134 |
The `DS.NumberTransform` class is used to serialize and deserialize |
|
1135 |
numeric attributes on Ember Data record objects. This transform is |
|
1136 |
used when `number` is passed as the type parameter to the |
|
1137 |
[DS.attr](../../data#method_attr) function. |
|
1138 |
|
|
1139 |
Usage |
|
1140 |
|
|
1141 |
```javascript |
|
1142 |
var attr = DS.attr; |
|
1143 |
App.Score = DS.Model.extend({ |
|
1144 |
value: attr('number'), |
|
1145 |
player: DS.belongsTo('player'), |
|
1146 |
date: attr('date') |
|
1147 |
}); |
|
1148 |
``` |
|
1149 |
|
|
1150 |
@class NumberTransform |
|
1151 |
@extends DS.Transform |
|
1152 |
@namespace DS |
|
1153 |
*/ |
|
1154 |
DS.NumberTransform = DS.Transform.extend({ |
|
1155 |
|
|
1156 |
deserialize: function(serialized) { |
|
1157 |
return empty(serialized) ? null : Number(serialized); |
|
1158 |
}, |
|
1159 |
|
|
1160 |
serialize: function(deserialized) { |
|
1161 |
return empty(deserialized) ? null : Number(deserialized); |
|
1162 |
} |
|
1163 |
}); |
|
1164 |
|
|
1165 |
})(); |
|
1166 |
|
|
1167 |
|
|
1168 |
|
|
1169 |
(function() { |
|
1170 |
var none = Ember.isNone; |
|
1171 |
|
|
1172 |
/** |
|
1173 |
The `DS.StringTransform` class is used to serialize and deserialize |
|
1174 |
string attributes on Ember Data record objects. This transform is |
|
1175 |
used when `string` is passed as the type parameter to the |
|
1176 |
[DS.attr](../../data#method_attr) function. |
|
1177 |
|
|
1178 |
Usage |
|
1179 |
|
|
1180 |
```javascript |
|
1181 |
var attr = DS.attr; |
|
1182 |
App.User = DS.Model.extend({ |
|
1183 |
isAdmin: attr('boolean'), |
|
1184 |
name: attr('string'), |
|
1185 |
email: attr('string') |
|
1186 |
}); |
|
1187 |
``` |
|
1188 |
|
|
1189 |
@class StringTransform |
|
1190 |
@extends DS.Transform |
|
1191 |
@namespace DS |
|
1192 |
*/ |
|
1193 |
DS.StringTransform = DS.Transform.extend({ |
|
1194 |
|
|
1195 |
deserialize: function(serialized) { |
|
1196 |
return none(serialized) ? null : String(serialized); |
|
1197 |
}, |
|
1198 |
|
|
1199 |
serialize: function(deserialized) { |
|
1200 |
return none(deserialized) ? null : String(deserialized); |
|
1201 |
} |
|
1202 |
|
|
1203 |
}); |
|
1204 |
|
|
1205 |
})(); |
|
1206 |
|
|
1207 |
|
|
1208 |
|
|
1209 |
(function() { |
|
1210 |
|
|
1211 |
})(); |
|
1212 |
|
|
1213 |
|
|
1214 |
|
|
1215 |
(function() { |
|
1216 |
/** |
|
1217 |
@module ember-data |
|
1218 |
*/ |
|
1219 |
|
|
1220 |
var set = Ember.set; |
|
1221 |
|
|
1222 |
/* |
|
1223 |
This code registers an injection for Ember.Application. |
|
1224 |
|
|
1225 |
If an Ember.js developer defines a subclass of DS.Store on their application, |
|
1226 |
this code will automatically instantiate it and make it available on the |
|
1227 |
router. |
|
1228 |
|
|
1229 |
Additionally, after an application's controllers have been injected, they will |
|
1230 |
each have the store made available to them. |
|
1231 |
|
|
1232 |
For example, imagine an Ember.js application with the following classes: |
|
1233 |
|
|
1234 |
App.Store = DS.Store.extend({ |
|
1235 |
adapter: 'custom' |
|
1236 |
}); |
|
1237 |
|
|
1238 |
App.PostsController = Ember.ArrayController.extend({ |
|
1239 |
// ... |
|
1240 |
}); |
|
1241 |
|
|
1242 |
When the application is initialized, `App.Store` will automatically be |
|
1243 |
instantiated, and the instance of `App.PostsController` will have its `store` |
|
1244 |
property set to that instance. |
|
1245 |
|
|
1246 |
Note that this code will only be run if the `ember-application` package is |
|
1247 |
loaded. If Ember Data is being used in an environment other than a |
|
1248 |
typical application (e.g., node.js where only `ember-runtime` is available), |
|
1249 |
this code will be ignored. |
|
1250 |
*/ |
|
1251 |
|
|
1252 |
Ember.onLoad('Ember.Application', function(Application) { |
|
1253 |
Application.initializer({ |
|
1254 |
name: "store", |
|
1255 |
|
|
1256 |
initialize: function(container, application) { |
|
1257 |
application.register('store:main', application.Store || DS.Store); |
|
1258 |
application.register('serializer:_default', DS.JSONSerializer); |
|
1259 |
application.register('serializer:_rest', DS.RESTSerializer); |
|
1260 |
application.register('adapter:_rest', DS.RESTAdapter); |
|
1261 |
|
|
1262 |
// Eagerly generate the store so defaultStore is populated. |
|
1263 |
// TODO: Do this in a finisher hook |
|
1264 |
container.lookup('store:main'); |
|
1265 |
} |
|
1266 |
}); |
|
1267 |
|
|
1268 |
Application.initializer({ |
|
1269 |
name: "transforms", |
|
1270 |
before: "store", |
|
1271 |
|
|
1272 |
initialize: function(container, application) { |
|
1273 |
application.register('transform:boolean', DS.BooleanTransform); |
|
1274 |
application.register('transform:date', DS.DateTransform); |
|
1275 |
application.register('transform:number', DS.NumberTransform); |
|
1276 |
application.register('transform:string', DS.StringTransform); |
|
1277 |
} |
|
1278 |
}); |
|
1279 |
|
|
1280 |
Application.initializer({ |
|
1281 |
name: "dataAdapter", |
|
1282 |
before: "store", |
|
1283 |
|
|
1284 |
initialize: function(container, application) { |
|
1285 |
application.register('dataAdapter:main', DS.DebugAdapter); |
|
1286 |
} |
|
1287 |
}); |
|
1288 |
|
|
1289 |
Application.initializer({ |
|
1290 |
name: "injectStore", |
|
1291 |
before: "store", |
|
1292 |
|
|
1293 |
initialize: function(container, application) { |
|
1294 |
application.inject('controller', 'store', 'store:main'); |
|
1295 |
application.inject('route', 'store', 'store:main'); |
|
1296 |
application.inject('serializer', 'store', 'store:main'); |
|
1297 |
application.inject('dataAdapter', 'store', 'store:main'); |
|
1298 |
} |
|
1299 |
}); |
|
1300 |
|
|
1301 |
}); |
|
1302 |
|
|
1303 |
})(); |
|
1304 |
|
|
1305 |
|
|
1306 |
|
|
1307 |
(function() { |
|
1308 |
/** |
|
1309 |
@module ember-data |
|
1310 |
*/ |
|
1311 |
|
|
1312 |
/** |
|
1313 |
Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601> |
|
1314 |
|
|
1315 |
© 2011 Colin Snover <http://zetafleet.com> |
|
1316 |
|
|
1317 |
Released under MIT license. |
|
1318 |
|
|
1319 |
@class Date |
|
1320 |
@namespace Ember |
|
1321 |
@static |
|
1322 |
*/ |
|
1323 |
Ember.Date = Ember.Date || {}; |
|
1324 |
|
|
1325 |
var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ]; |
|
1326 |
|
|
1327 |
/** |
|
1328 |
@method parse |
|
1329 |
@param date |
|
1330 |
*/ |
|
1331 |
Ember.Date.parse = function (date) { |
|
1332 |
var timestamp, struct, minutesOffset = 0; |
|
1333 |
|
|
1334 |
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string |
|
1335 |
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native |
|
1336 |
// implementations could be faster |
|
1337 |
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm |
|
1338 |
if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) { |
|
1339 |
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC |
|
1340 |
for (var i = 0, k; (k = numericKeys[i]); ++i) { |
|
1341 |
struct[k] = +struct[k] || 0; |
|
1342 |
} |
|
1343 |
|
|
1344 |
// allow undefined days and months |
|
1345 |
struct[2] = (+struct[2] || 1) - 1; |
|
1346 |
struct[3] = +struct[3] || 1; |
|
1347 |
|
|
1348 |
if (struct[8] !== 'Z' && struct[9] !== undefined) { |
|
1349 |
minutesOffset = struct[10] * 60 + struct[11]; |
|
1350 |
|
|
1351 |
if (struct[9] === '+') { |
|
1352 |
minutesOffset = 0 - minutesOffset; |
|
1353 |
} |
|
1354 |
} |
|
1355 |
|
|
1356 |
timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]); |
|
1357 |
} |
|
1358 |
else { |
|
1359 |
timestamp = origParse ? origParse(date) : NaN; |
|
1360 |
} |
|
1361 |
|
|
1362 |
return timestamp; |
|
1363 |
}; |
|
1364 |
|
|
1365 |
if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.Date) { |
|
1366 |
Date.parse = Ember.Date.parse; |
|
1367 |
} |
|
1368 |
|
|
1369 |
})(); |
|
1370 |
|
|
1371 |
|
|
1372 |
|
|
1373 |
(function() { |
|
1374 |
|
|
1375 |
})(); |
|
1376 |
|
|
1377 |
|
|
1378 |
|
|
1379 |
(function() { |
|
1380 |
/** |
|
1381 |
@module ember-data |
|
1382 |
*/ |
|
1383 |
|
|
1384 |
var get = Ember.get, set = Ember.set; |
|
1385 |
|
|
1386 |
/** |
|
1387 |
A record array is an array that contains records of a certain type. The record |
|
1388 |
array materializes records as needed when they are retrieved for the first |
|
1389 |
time. You should not create record arrays yourself. Instead, an instance of |
|
1390 |
`DS.RecordArray` or its subclasses will be returned by your application's store |
|
1391 |
in response to queries. |
|
1392 |
|
|
1393 |
@class RecordArray |
|
1394 |
@namespace DS |
|
1395 |
@extends Ember.ArrayProxy |
|
1396 |
@uses Ember.Evented |
|
1397 |
*/ |
|
1398 |
|
|
1399 |
DS.RecordArray = Ember.ArrayProxy.extend(Ember.Evented, { |
|
1400 |
/** |
|
1401 |
The model type contained by this record array. |
|
1402 |
|
|
1403 |
@property type |
|
1404 |
@type DS.Model |
|
1405 |
*/ |
|
1406 |
type: null, |
|
1407 |
|
|
1408 |
/** |
|
1409 |
The array of client ids backing the record array. When a |
|
1410 |
record is requested from the record array, the record |
|
1411 |
for the client id at the same index is materialized, if |
|
1412 |
necessary, by the store. |
|
1413 |
|
|
1414 |
@property content |
|
1415 |
@private |
|
1416 |
@type Ember.Array |
|
1417 |
*/ |
|
1418 |
content: null, |
|
1419 |
|
|
1420 |
/** |
|
1421 |
The flag to signal a `RecordArray` is currently loading data. |
|
1422 |
|
|
1423 |
Example |
|
1424 |
|
|
1425 |
```javascript |
|
1426 |
var people = store.all(App.Person); |
|
1427 |
people.get('isLoaded'); // true |
|
1428 |
``` |
|
1429 |
|
|
1430 |
@property isLoaded |
|
1431 |
@type Boolean |
|
1432 |
*/ |
|
1433 |
isLoaded: false, |
|
1434 |
/** |
|
1435 |
The flag to signal a `RecordArray` is currently loading data. |
|
1436 |
|
|
1437 |
Example |
|
1438 |
|
|
1439 |
```javascript |
|
1440 |
var people = store.all(App.Person); |
|
1441 |
people.get('isUpdating'); // false |
|
1442 |
people.update(); |
|
1443 |
people.get('isUpdating'); // true |
|
1444 |
``` |
|
1445 |
|
|
1446 |
@property isUpdating |
|
1447 |
@type Boolean |
|
1448 |
*/ |
|
1449 |
isUpdating: false, |
|
1450 |
|
|
1451 |
/** |
|
1452 |
The store that created this record array. |
|
1453 |
|
|
1454 |
@property store |
|
1455 |
@private |
|
1456 |
@type DS.Store |
|
1457 |
*/ |
|
1458 |
store: null, |
|
1459 |
|
|
1460 |
/** |
|
1461 |
Retrieves an object from the content by index. |
|
1462 |
|
|
1463 |
@method objectAtContent |
|
1464 |
@private |
|
1465 |
@param {Number} index |
|
1466 |
@return {DS.Model} record |
|
1467 |
*/ |
|
1468 |
objectAtContent: function(index) { |
|
1469 |
var content = get(this, 'content'); |
|
1470 |
|
|
1471 |
return content.objectAt(index); |
|
1472 |
}, |
|
1473 |
|
|
1474 |
/** |
|
1475 |
Used to get the latest version of all of the records in this array |
|
1476 |
from the adapter. |
|
1477 |
|
|
1478 |
Example |
|
1479 |
|
|
1480 |
```javascript |
|
1481 |
var people = store.all(App.Person); |
|
1482 |
people.get('isUpdating'); // false |
|
1483 |
people.update(); |
|
1484 |
people.get('isUpdating'); // true |
|
1485 |
``` |
|
1486 |
|
|
1487 |
@method update |
|
1488 |
*/ |
|
1489 |
update: function() { |
Also available in: Unified diff