Commit d6c38b45 authored by Pascal Hartig's avatar Pascal Hartig

Ember: Upgrade to 1.6.0

parent 5a53ef13
......@@ -5,7 +5,7 @@
"todomvc-common": "~0.1.4",
"jquery": "~2.1.0",
"handlebars": "~1.3.0",
"ember": "~1.5.0",
"ember": "~1.6.0",
"ember-data": "1.0.0-beta.7",
"ember-localstorage-adapter": "latest"
}
......
/*global Ember*/
/*global DS*/
'use strict';
(function () {
'use strict';
DS.LSSerializer = DS.JSONSerializer.extend({
DS.LSSerializer = DS.JSONSerializer.extend({
serializeHasMany: function(record, json, relationship) {
var key = relationship.key,
......@@ -16,10 +17,46 @@ DS.LSSerializer = DS.JSONSerializer.extend({
}
},
/**
* Extracts whatever was returned from the adapter.
*
* If the adapter returns relationships in an embedded way, such as follows:
*
* ```js
* {
* "id": 1,
* "title": "Rails Rambo",
*
* "_embedded": {
* "comment": [{
* "id": 1,
* "comment_title": "FIRST"
* }, {
* "id": 2,
* "comment_title": "Rails is unagi"
* }]
* }
* }
*
* this method will create separated JSON for each resource and then push
* them individually to the Store.
*
* In the end, only the main resource will remain, containing the ids of its
* relationships. Given the relations are already in the Store, we will
* return a JSON with the main resource alone. The Store will sort out the
* associations by itself.
*
* @method extractSingle
* @private
* @param {DS.Store} store the returned store
* @param {DS.Model} type the type/model
* @param {Object} payload returned JSON
*/
extractSingle: function(store, type, payload) {
if (payload && payload._embedded) {
for (var relation in payload._embedded) {
var typeName = Ember.String.singularize(relation),
var relType = type.typeForRelationship(relation);
var typeName = relType.typeKey,
embeddedPayload = payload._embedded[relation];
if (embeddedPayload) {
......@@ -35,11 +72,29 @@ DS.LSSerializer = DS.JSONSerializer.extend({
}
return this.normalize(type, payload);
},
/**
* This is exactly the same as extractSingle, but used in an array.
*
* @method extractSingle
* @private
* @param {DS.Store} store the returned store
* @param {DS.Model} type the type/model
* @param {Array} payload returned JSONs
*/
extractArray: function(store, type, payload) {
var serializer = this;
return payload.map(function(record) {
var extracted = serializer.extractSingle(store, type, record);
return serializer.normalize(type, record);
});
}
});
});
DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
/**
This is the main entry point into finding records. The first parameter to
this method is the model's name as a string.
......@@ -68,20 +123,20 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
return new Ember.RSVP.Promise(function(resolve, reject) {
var record = Ember.A(namespace.records[id]);
if (allowRecursive && record) {
if (!record || !record.hasOwnProperty('id')) {
store.dematerializeRecord(store.typeMapFor(type).idToRecord[id]);
reject();
return;
}
if (allowRecursive) {
adapter.loadRelationships(type, record).then(function(finalRecord) {
resolve(finalRecord);
});
} else {
if (!record) {
reject();
} else {
resolve(record);
}
}
});
resolve(record);
},
findMany: function (store, type, ids) {
......@@ -96,6 +151,12 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
}
resolve(results);
}).then(function(records) {
if (records.get('length')) {
return adapter.loadRelationshipsForMany(type, records);
} else {
return records;
}
});
},
......@@ -117,7 +178,12 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
var namespace = this._namespaceForType(type),
results = this.query(namespace.records, query);
if (results.get('length')) {
results = this.loadRelationshipsForMany(type, results);
return Ember.RSVP.resolve(results);
} else {
return Ember.RSVP.reject();
}
},
query: function (records, query) {
......@@ -188,7 +254,7 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
// private
adapterNamespace: function () {
return this.namespace || 'DS.LSAdapter';
return this.get('namespace') || 'DS.LSAdapter';
},
loadData: function () {
......@@ -213,7 +279,7 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
},
modelNamespace: function(type) {
return type.url || type.toString();
return type.url || type.typeKey;
},
......@@ -392,6 +458,58 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
return Object.prototype.toString.call(value) === '[object Array]';
},
/**
* Same as `loadRelationships`, but for an array of records.
*
* @method loadRelationshipsForMany
* @private
* @param {DS.Model} type
* @param {Object} recordsArray
*/
loadRelationshipsForMany: function(type, recordsArray) {
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var recordsWithRelationships = [],
recordsToBeLoaded = [],
promises = [];
/**
* Some times Ember puts some stuff in arrays. We want to clean it so
* we know exactly what to iterate over.
*/
for (var i in recordsArray) {
if (recordsArray.hasOwnProperty(i)) {
recordsToBeLoaded.push(recordsArray[i]);
}
}
var loadNextRecord = function(record) {
/**
* Removes the first item from recordsToBeLoaded
*/
recordsToBeLoaded = recordsToBeLoaded.slice(1);
var promise = adapter.loadRelationships(type, record);
promise.then(function(recordWithRelationships) {
recordsWithRelationships.push(recordWithRelationships);
if (recordsToBeLoaded[0]) {
loadNextRecord(recordsToBeLoaded[0]);
} else {
resolve(recordsWithRelationships);
}
});
}
/**
* We start by the first record
*/
loadNextRecord(recordsToBeLoaded[0]);
});
},
/**
*
......@@ -408,4 +526,5 @@ DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
return relationships;
}
}
});
});
}());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment