Skip to content

Commit

Permalink
Further WIP on embedded records
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhuda committed Dec 18, 2012
1 parent 301b3fc commit beb2f26
Show file tree
Hide file tree
Showing 22 changed files with 486 additions and 321 deletions.
179 changes: 94 additions & 85 deletions packages/ember-data/lib/adapters/rest_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,58 @@ require('ember-data/system/adapter');
require('ember-data/serializers/rest_serializer');
/*global jQuery*/

var get = Ember.get, set = Ember.set;
var get = Ember.get, set = Ember.set, merge = Ember.merge;

function loaderFor(store, record, id) {
return {
acknowledge: function() {
store.didSaveRecord(record);
},

loadMain: function(type, data, prematerialized) {
if (record) {
store.didSaveRecord(record, data, prematerialized);
} else {
if (id) {
prematerialized = prematerialized || {};
prematerialized.id = id;
}

return store.load(type, data, prematerialized);
}
},

load: function() {
return store.load.apply(store, arguments);
},

loadMany: function(type, array) {
for (var i=0, l=array.length; i<l; i++) {
store.load(type, array[i]);
}
},

sinceForType: function(type, since) {
store.sinceForType(type, since);
}
};
}

DS.RESTAdapter = DS.Adapter.extend({
bulkCommit: false,
since: 'since',

serializer: DS.RESTSerializer,

init: function() {
this._super.apply(this, arguments);

get(this, 'serializer').plurals = this.plurals;
get(this, 'serializer').plurals = this.plurals || {};
},

load: function(store, type, payload) {
var loader = loaderFor(store, null, this.extractId(type, payload));
get(this, 'serializer').extractSingle(loader, type, payload);
},

createRecord: function(store, type, record) {
Expand All @@ -27,7 +68,7 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didCreateRecord(store, type, record, json);
this.didSaveRecord(store, type, record, json);
});
},
error: function(xhr) {
Expand All @@ -38,36 +79,32 @@ DS.RESTAdapter = DS.Adapter.extend({

dirtyRecordsForHasManyChange: Ember.K,

didSaveRecord: function(store, record, hash) {
record.eachAssociation(function(name, meta) {
if (meta.kind === 'belongsTo') {
store.didUpdateRelationship(record, name);
}
});
didSaveRecord: function(store, type, record, json) {
var loader = loaderFor(store, record);

store.didSaveRecord(record, hash);
get(this, 'serializer').extract(loader, json, {
type: type
});
},

didSaveRecords: function(store, records, array) {
var i = 0;
didSaveRecords: function(store, type, records, json) {
var self = this;

records.forEach(function(record) {
this.didSaveRecord(store, record, array && array[i++]);
}, this);
},
var loader = loaderFor(store);
loader.loadMainArray = function(type, array) {
var i = 0;

didCreateRecord: function(store, type, record, json) {
var loader = {
loadMain: function(type, data, prematerialized) {
store.didSaveRecord(record, data, prematerialized);
},
records.forEach(function(record) {
store.didSaveRecord(record, array && array[i++]);
});
};

load: function() {
store.load.apply(store, arguments);
}
loader.acknowledge = function() {
records.forEach(store.didSaveRecord, store);
};

get(this, 'serializer').extract(loader, json, {
multiple: true,
type: type
});
},
Expand All @@ -91,19 +128,12 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didCreateRecords(store, type, records, json);
this.didSaveRecords(store, type, records, json);
});
}
});
},

didCreateRecords: function(store, type, records, json) {
var root = this.pluralize(this.rootForType(type));

this.sideload(store, type, json, root);
this.didSaveRecords(store, records, json[root]);
},

updateRecord: function(store, type, record) {
var id = get(record, 'id');
var root = this.rootForType(type);
Expand All @@ -116,7 +146,7 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didUpdateRecord(store, type, record, json);
this.didSaveRecord(store, type, record, json);
});
},
error: function(xhr) {
Expand All @@ -125,13 +155,6 @@ DS.RESTAdapter = DS.Adapter.extend({
});
},

didUpdateRecord: function(store, type, record, json) {
var root = this.rootForType(type);

this.sideload(store, type, json, root);
this.didSaveRecord(store, record, json && json[root]);
},

updateRecords: function(store, type, records) {
if (get(this, 'bulkCommit') === false) {
return this._super(store, type, records);
Expand All @@ -151,19 +174,12 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didUpdateRecords(store, type, records, json);
this.didSaveRecords(store, type, records, json);
});
}
});
},

didUpdateRecords: function(store, type, records, json) {
var root = this.pluralize(this.rootForType(type));

this.sideload(store, type, json, root);
this.didSaveRecords(store, records, json[root]);
},

deleteRecord: function(store, type, record) {
var id = get(record, 'id');
var root = this.rootForType(type);
Expand All @@ -172,17 +188,12 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didDeleteRecord(store, type, record, json);
this.didSaveRecord(store, type, record, json);
});
}
});
},

didDeleteRecord: function(store, type, record, json) {
if (json) { this.sideload(store, type, json); }
this.didSaveRecord(store, record);
},

deleteRecords: function(store, type, records) {
if (get(this, 'bulkCommit') === false) {
return this._super(store, type, records);
Expand All @@ -203,17 +214,12 @@ DS.RESTAdapter = DS.Adapter.extend({
context: this,
success: function(json) {
Ember.run(this, function(){
this.didDeleteRecords(store, type, records, json);
this.didSaveRecords(store, type, records, json);
});
}
});
},

didDeleteRecords: function(store, type, records, json) {
if (json) { this.sideload(store, type, json); }
this.didSaveRecords(store, records);
},

find: function(store, type, id) {
var root = this.rootForType(type);

Expand All @@ -227,19 +233,10 @@ DS.RESTAdapter = DS.Adapter.extend({
},

didFindRecord: function(store, type, json, id) {
var loader = {
loadMain: function() {
store.load.apply(store, arguments);
},
var loader = loaderFor(store, null, id);

load: function() {
store.load.apply(store, arguments);
}
};

this.extract(loader, json, {
type: type,
id: id
get(this, 'serializer').extract(loader, json, {
type: type
});
},

Expand All @@ -257,11 +254,16 @@ DS.RESTAdapter = DS.Adapter.extend({
},

didFindAll: function(store, type, json) {
var root = this.pluralize(this.rootForType(type)),
since = this.extractSince(json);
var loader = loaderFor(store),
serializer = get(this, 'serializer');

loader.loadMainArray = function(type, data) {
loader.loadMany(type, data);
};

serializer.extract(loader, json, { type: type, multiple: true });

this.sideload(store, type, json, root);
store.loadMany(type, json[root]);
var since = this.extractSince(json);

// this registers the id with the store, so it will be passed
// into the next call to `findAll`
Expand All @@ -284,10 +286,15 @@ DS.RESTAdapter = DS.Adapter.extend({
},

didFindQuery: function(store, type, json, recordArray) {
var root = this.pluralize(this.rootForType(type));
var loader = loaderFor(store);
loader.loadMainArray = function(type, data) {
recordArray.load(data);
};

this.sideload(store, type, json, root);
recordArray.load(json[root]);
get(this, 'serializer').extract(loader, json, {
multiple: true,
type: type
});
},

findMany: function(store, type, ids) {
Expand Down Expand Up @@ -320,10 +327,15 @@ DS.RESTAdapter = DS.Adapter.extend({
},

didFindMany: function(store, type, json) {
var root = this.pluralize(this.rootForType(type));
var loader = loaderFor(store);
loader.loadMainArray = function(type, data) {
loader.loadMany(type, data);
};

this.sideload(store, type, json, root);
store.loadMany(type, json[root]);
get(this, 'serializer').extract(loader, json, {
multiple: true,
type: type
});
},

didError: function(store, type, record, xhr) {
Expand Down Expand Up @@ -380,9 +392,6 @@ DS.RESTAdapter = DS.Adapter.extend({
return url.join("/");
},

meta: 'meta',
since: 'since',

sinceQuery: function(since) {
var query = {};
query[get(this, 'since')] = since;
Expand Down
Loading

0 comments on commit beb2f26

Please sign in to comment.