Skip to content

Commit

Permalink
Merge pull request #129 from Kilowhisky/ember-1.13.+
Browse files Browse the repository at this point in the history
Ember 1.13 Support
  • Loading branch information
kurko committed Aug 27, 2015
2 parents 1726db6 + 06339a4 commit c321b47
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 62 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"vendor"
],
"dependencies": {
"ember-data": ">= 1.0.0-beta.16.1",
"ember": ">= 1.9.0"
"ember-data": "1.13.5",
"ember": ">= 1.12.0"
}
}
81 changes: 52 additions & 29 deletions localstorage_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
'use strict';

DS.LSSerializer = DS.JSONSerializer.extend({

/**
* Invokes the new serializer API.
* This should be removed in 2.0
*/
isNewSerializerAPI: true,

serializeHasMany: function(snapshot, json, relationship) {
var key = relationship.key;
var payloadKey = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
var relationshipType = snapshot.type.determineRelationshipType(relationship);
var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);

if (relationshipType === 'manyToNone' ||
relationshipType === 'manyToMany' ||
Expand All @@ -17,8 +23,8 @@
// TODO support for polymorphic manyToNone and manyToMany relationships
}
},

/**
/**
* Extracts whatever was returned from the adapter.
*
* If the adapter returns relationships in an embedded way, such as follows:
Expand All @@ -39,21 +45,19 @@
* }
* }
*
* 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.
* this method will create separated JSON for each resource and then combine
* the data and the embed payload into the JSON.Api spec for included objects
* returning a single object.
*
* @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) {
normalizeSingleResponse: function(store, type, payload) {
var included = [];

if (payload && payload._embedded) {
for (var relation in payload._embedded) {
var relType = type.typeForRelationship(relation,store);
Expand All @@ -62,20 +66,26 @@

if (embeddedPayload) {
if (Ember.isArray(embeddedPayload)) {
store.pushMany(typeName, embeddedPayload);
embeddedPayload.forEach(function(record) {
included.pushObject(this.normalize(relType,record).data);
}.bind(this));
} else {
store.push(typeName, embeddedPayload);
included.pushObject(this.normalize(relType, embeddedPayload).data);
}
}
}

delete payload._embedded;
}

return this.normalize(type, payload);
var normalPayload = this.normalize(type, payload);
if(included.length > 0){
normalPayload['included'] = included;
}
return normalPayload;
},

/**
/**
* This is exactly the same as extractSingle, but used in an array.
*
* @method extractSingle
Expand All @@ -84,10 +94,23 @@
* @param {DS.Model} type the type/model
* @param {Array} payload returned JSONs
*/
extractArray: function(store, type, payload) {
return payload.map(function(json) {
return this.extractSingle(store, type, json);
}, this);
normalizeArrayResponse: function(store, type, payload) {
var response = { data: [], included: [] };

payload.forEach(function(json){
var normalized = this.normalizeSingleResponse(store, type, json);
response.data.pushObject(normalized.data);

if(normalized.included){
normalized.included.forEach(function(included){
if(!response.included.contains(included.id)){
response.included.addObject(included);
}
});
}
}.bind(this));

return response;
}

});
Expand All @@ -101,7 +124,7 @@
@param {DS.Model} type
@param {Object|String|Integer|null} id
*/
find: function(store, type, id, opts) {
findRecord: function(store, type, id, opts) {
var allowRecursive = true;
var namespace = this._namespaceForType(type);
var record = Ember.A(namespace.records[id]);
Expand Down Expand Up @@ -179,9 +202,9 @@
// match records with "complete: true" and the name "foo" or "bar"
//
// { complete: true, name: /foo|bar/ }
findQuery: function (store, type, query, recordArray) {
query: function (store, type, query, recordArray) {
var namespace = this._namespaceForType(type);
var results = this.query(namespace.records, query);
var results = this._query(namespace.records, query);

if (results.get('length')) {
return this.loadRelationshipsForMany(store, type, results);
Expand All @@ -190,11 +213,11 @@
}
},

query: function (records, query) {
_query: function (records, query) {
var results = [], record;

function recordMatchesQuery(record) {
return Ember.keys(query).every(function(property) {
return Object.keys(query).every(function(property) {
var test = query[property];
if (Object.prototype.toString.call(test) === '[object RegExp]') {
return test.test(record[property]);
Expand Down Expand Up @@ -387,7 +410,7 @@
var relationEmbeddedId = record[relationName];
var relationProp = adapter.relationshipProperties(type, relationName);
var relationType = relationProp.kind;
var foreignAdapter = store.adapterFor(relationName);
var foreignAdapter = store.adapterFor(relationModel.modelName);

var opts = {allowRecursive: false};

Expand All @@ -405,12 +428,12 @@
* 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 && foreignAdapter === adapter)
if (relationEmbeddedId && DS.LSAdapter.prototype.isPrototypeOf(adapter))
{
recordPromise = recordPromise.then(function(recordPayload) {
var promise;
if (relationType === 'belongsTo' || relationType === 'hasOne') {
promise = adapter.find(null, relationModel, relationEmbeddedId, opts);
promise = adapter.findRecord(null, relationModel, relationEmbeddedId, opts);
} else if (relationType == 'hasMany') {
promise = adapter.findMany(null, relationModel, relationEmbeddedId, opts);
}
Expand Down Expand Up @@ -465,7 +488,7 @@
*/
addEmbeddedPayload: function(payload, relationshipName, relationshipRecord) {
var objectHasId = (relationshipRecord && relationshipRecord.id);
var arrayHasIds = (relationshipRecord.length && relationshipRecord.everyBy("id"));
var arrayHasIds = (relationshipRecord.length && relationshipRecord.isEvery("id"));
var isValidRelationship = (objectHasId || arrayHasIds);

if (isValidRelationship) {
Expand Down
Loading

0 comments on commit c321b47

Please sign in to comment.