Commit 258e1720 authored by Pascal Hartig's avatar Pascal Hartig

Ember: Soft-upgrade 1.6 dependencies

parent 12b68729
......@@ -6,13 +6,14 @@
DS.LSSerializer = DS.JSONSerializer.extend({
serializeHasMany: function(record, json, relationship) {
var key = relationship.key,
relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);
var key = relationship.key;
var payloadKey = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);
if (relationshipType === 'manyToNone' ||
relationshipType === 'manyToMany' ||
relationshipType === 'manyToOne') {
json[key] = record.get(key).mapBy('id');
json[payloadKey] = record.get(key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany relationships
}
},
......@@ -84,12 +85,9 @@
* @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);
});
return payload.map(function(json) {
return this.extractSingle(store, type, json);
}, this);
}
});
......@@ -104,9 +102,9 @@
@param {Object|String|Integer|null} id
*/
find: function(store, type, id, opts) {
var adapter = this;
var allowRecursive = true;
var namespace = this._namespaceForType(type);
var record = Ember.A(namespace.records[id]);
/**
* In the case where there are relationships, this method is called again
......@@ -120,44 +118,51 @@
allowRecursive = opts.allowRecursive;
}
return new Ember.RSVP.Promise(function(resolve, reject) {
var record = Ember.A(namespace.records[id]);
if (!record || !record.hasOwnProperty('id')) {
store.dematerializeRecord(store.typeMapFor(type).idToRecord[id]);
reject();
return;
return Ember.RSVP.reject(new Error("Couldn't find record of"
+ " type '" + type.typeKey
+ "' for the id '" + id + "'."));
}
if (allowRecursive) {
adapter.loadRelationships(type, record).then(function(finalRecord) {
resolve(finalRecord);
});
return this.loadRelationships(type, record);
} else {
resolve(record);
return Ember.RSVP.resolve(record);
}
});
},
findMany: function (store, type, ids) {
var adapter = this;
findMany: function (store, type, ids, opts) {
var namespace = this._namespaceForType(type);
var adapter = this,
allowRecursive = true,
results = [], record;
return new Ember.RSVP.Promise(function(resolve, reject) {
var results = [];
/**
* In the case where there are relationships, this method is called again
* for each relation. Given the relations have references to the main
* object, we use allowRecursive to avoid going further into infinite
* recursiveness.
*
* Concept from ember-indexdb-adapter
*/
if (opts && typeof opts.allowRecursive !== 'undefined') {
allowRecursive = opts.allowRecursive;
}
for (var i = 0; i < ids.length; i++) {
results.push(Ember.copy(namespace.records[ids[i]]));
record = namespace.records[ids[i]];
if (!record || !record.hasOwnProperty('id')) {
return Ember.RSVP.reject(new Error("Couldn't find record of type '" + type.typeKey
+ "' for the id '" + ids[i] + "'."));
}
results.push(Ember.copy(record));
}
resolve(results);
}).then(function(records) {
if (records.get('length')) {
return adapter.loadRelationshipsForMany(type, records);
if (results.get('length') && allowRecursive) {
return this.loadRelationshipsForMany(type, results);
} else {
return records;
return Ember.RSVP.resolve(results);
}
});
},
// Supports queries that look like this:
......@@ -175,33 +180,34 @@
//
// { complete: true, name: /foo|bar/ }
findQuery: function (store, type, query, recordArray) {
var namespace = this._namespaceForType(type),
results = this.query(namespace.records, query);
var namespace = this._namespaceForType(type);
var results = this.query(namespace.records, query);
if (results.get('length')) {
results = this.loadRelationshipsForMany(type, results);
return Ember.RSVP.resolve(results);
return this.loadRelationshipsForMany(type, results);
} else {
return Ember.RSVP.reject();
}
},
query: function (records, query) {
var results = [],
id, record, property, test, push;
for (id in records) {
record = records[id];
for (property in query) {
test = query[property];
push = false;
var results = [], record;
function recordMatchesQuery(record) {
return Ember.keys(query).every(function(property) {
var test = query[property];
if (Object.prototype.toString.call(test) === '[object RegExp]') {
push = test.test(record[property]);
return test.test(record[property]);
} else {
push = record[property] === test;
return record[property] === test;
}
});
}
if (push) {
results.push(record);
for (var id in records) {
record = records[id];
if (recordMatchesQuery(record)) {
results.push(Ember.copy(record));
}
}
return results;
......@@ -218,8 +224,8 @@
},
createRecord: function (store, type, record) {
var namespaceRecords = this._namespaceForType(type),
recordHash = record.serialize({includeId: true});
var namespaceRecords = this._namespaceForType(type);
var recordHash = record.serialize({includeId: true});
namespaceRecords.records[recordHash.id] = recordHash;
......@@ -228,8 +234,8 @@
},
updateRecord: function (store, type, record) {
var namespaceRecords = this._namespaceForType(type),
id = record.get('id');
var namespaceRecords = this._namespaceForType(type);
var id = record.get('id');
namespaceRecords.records[id] = record.serialize({ includeId: true });
......@@ -238,8 +244,8 @@
},
deleteRecord: function (store, type, record) {
var namespaceRecords = this._namespaceForType(type),
id = record.get('id');
var namespaceRecords = this._namespaceForType(type);
var id = record.get('id');
delete namespaceRecords.records[id];
......@@ -263,8 +269,8 @@
},
persistData: function(type, data) {
var modelNamespace = this.modelNamespace(type),
localStorageData = this.loadData();
var modelNamespace = this.modelNamespace(type);
var localStorageData = this.loadData();
localStorageData[modelNamespace] = data;
......@@ -272,10 +278,10 @@
},
_namespaceForType: function (type) {
var namespace = this.modelNamespace(type),
storage = localStorage.getItem(this.adapterNamespace());
var namespace = this.modelNamespace(type);
var storage = this.loadData();
return storage ? JSON.parse(storage)[namespace] || {records: {}} : {records: {}};
return storage[namespace] || {records: {}};
},
modelNamespace: function(type) {
......@@ -326,27 +332,29 @@
* @param {Object} record
*/
loadRelationships: function(type, record) {
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var resultJSON = {},
var adapter = this,
resultJSON = {},
typeKey = type.typeKey,
relationshipNames, relationships,
relationshipPromises = [];
/**
* Create a chain of promises, so the relationships are
* loaded sequentially. Think of the variable
* `recordPromise` as of the accumulator in a left fold.
*/
var recordPromise = Ember.RSVP.resolve(record);
relationshipNames = Ember.get(type, 'relationshipNames');
relationships = relationshipNames.belongsTo;
relationships = relationships.concat(relationshipNames.hasMany);
relationships = relationshipNames.belongsTo
.concat(relationshipNames.hasMany);
relationships.forEach(function(relationName) {
var relationModel = type.typeForRelationship(relationName),
relationEmbeddedId = record[relationName],
relationProp = adapter.relationshipProperties(type, relationName),
relationType = relationProp.kind,
/**
* This is the relationship field.
*/
promise, embedPromise;
var relationModel = type.typeForRelationship(relationName);
var relationEmbeddedId = record[relationName];
var relationProp = adapter.relationshipProperties(type, relationName);
var relationType = relationProp.kind;
var foreignAdapter = type.store.adapterFor(relationModel);
var opts = {allowRecursive: false};
......@@ -364,28 +372,24 @@
* In this case, cart belongsTo customer and its id is present in the
* main payload. We find each of these records and add them to _embedded.
*/
if (relationEmbeddedId) {
if (relationType == 'belongsTo' || relationType == 'hasOne') {
promise = adapter.find(null, relationModel, relationEmbeddedId, opts)
if (relationEmbeddedId && foreignAdapter === adapter)
{
recordPromise = recordPromise.then(function(recordPayload) {
var promise;
if (relationType === 'belongsTo' || relationType === 'hasOne') {
promise = adapter.find(null, relationModel, relationEmbeddedId, opts);
} else if (relationType == 'hasMany') {
promise = adapter.findMany(null, relationModel, relationEmbeddedId, opts)
promise = adapter.findMany(null, relationModel, relationEmbeddedId, opts);
}
embedPromise = new Ember.RSVP.Promise(function(resolve, reject) {
promise.then(function(relationRecord) {
var finalPayload = adapter.addEmbeddedPayload(record, relationName, relationRecord)
resolve(finalPayload);
return promise.then(function(relationRecord) {
return adapter.addEmbeddedPayload(recordPayload, relationName, relationRecord);
});
});
relationshipPromises.push(embedPromise);
}
});
Ember.RSVP.all(relationshipPromises).then(function() {
resolve(record);
});
});
return recordPromise;
},
......@@ -427,13 +431,13 @@
* @param {Object} relationshipRecord
*/
addEmbeddedPayload: function(payload, relationshipName, relationshipRecord) {
var objectHasId = (relationshipRecord && relationshipRecord.id),
arrayHasIds = (relationshipRecord.length && relationshipRecord.everyBy("id")),
isValidRelationship = (objectHasId || arrayHasIds);
var objectHasId = (relationshipRecord && relationshipRecord.id);
var arrayHasIds = (relationshipRecord.length && relationshipRecord.everyBy("id"));
var isValidRelationship = (objectHasId || arrayHasIds);
if (isValidRelationship) {
if (!payload['_embedded']) {
payload['_embedded'] = {}
payload['_embedded'] = {};
}
payload['_embedded'][relationshipName] = relationshipRecord;
......@@ -467,47 +471,24 @@
* @param {Object} recordsArray
*/
loadRelationshipsForMany: function(type, recordsArray) {
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var recordsWithRelationships = [],
recordsToBeLoaded = [],
promises = [];
var adapter = this,
promise = Ember.RSVP.resolve([]);
/**
* Some times Ember puts some stuff in arrays. We want to clean it so
* we know exactly what to iterate over.
* Create a chain of promises, so the records are loaded sequentially.
* Think of the variable promise as of the accumulator in a left fold.
*/
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);
}
recordsArray.forEach(function(record) {
promise = promise.then(function(records) {
return adapter.loadRelationships(type, record)
.then(function(loadedRecord) {
records.push(loadedRecord);
return records;
});
});
}
/**
* We start by the first record
*/
loadNextRecord(recordsToBeLoaded[0]);
});
return promise;
},
......
......@@ -5,7 +5,7 @@
* Portions Copyright 2008-2011 Apple Inc. All rights reserved.
* @license Licensed under MIT license
* See https://raw.github.com/emberjs/ember.js/master/LICENSE
* @version 1.6.0
* @version 1.6.1
*/
......@@ -2518,7 +2518,7 @@ define("ember-metal/core",
@class Ember
@static
@version 1.6.0
@version 1.6.1
*/
if ('undefined' === typeof Ember) {
......@@ -2545,10 +2545,10 @@ define("ember-metal/core",
/**
@property VERSION
@type String
@default '1.6.0'
@default '1.6.1'
@static
*/
Ember.VERSION = '1.6.0';
Ember.VERSION = '1.6.1';
/**
Standard environmental variables. You can define these in a global `EmberENV`
......@@ -39379,7 +39379,7 @@ define("ember-routing/system/router",
} else if (error.name === 'TransitionAborted') {
// just ignore TransitionAborted here
} else {
throw error;
logError(error);
}
return error;
......@@ -39522,16 +39522,7 @@ define("ember-routing/system/router",
return;
}
var errorArgs = ['Error while processing route: ' + transition.targetName];
if (error) {
if (error.message) { errorArgs.push(error.message); }
if (error.stack) { errorArgs.push(error.stack); }
if (typeof error === "string") { errorArgs.push(error); }
}
Ember.Logger.error.apply(this, errorArgs);
logError(error, 'Error while processing route: ' + transition.targetName);
},
loading: function(transition, originRoute) {
......@@ -39562,6 +39553,21 @@ define("ember-routing/system/router",
}
};
function logError(error, initialMessage) {
var errorArgs = [];
if (initialMessage) { errorArgs.push(initialMessage); }
if (error) {
if (error.message) { errorArgs.push(error.message); }
if (error.stack) { errorArgs.push(error.stack); }
if (typeof error === "string") { errorArgs.push(error); }
}
Ember.Logger.error.apply(this, errorArgs);
}
function findChildRouteName(parentRoute, originatingChildRoute, name) {
var router = parentRoute.router,
childName,
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