Permalink
Browse files

Support create/update/delete

  • Loading branch information...
1 parent 57d6c01 commit 53e14dd382556a5ae2ee6c01355ca030eebe39fb tomhuda committed Mar 22, 2013
@@ -157,18 +157,20 @@ function hasManyProcessorFactory(store, record, relationship) {
};
}
-function CreateProcessor(record, store, type) {
+function SaveProcessor(record, store, type, includeId) {
this.record = record;
- ObjectProcessor.call(this, record.toJSON(), type, store);
+ ObjectProcessor.call(this, record.toJSON({ includeId: includeId }), type, store);
}
-CreateProcessor.prototype = Ember.create(ObjectProcessor.prototype);
+SaveProcessor.prototype = Ember.create(ObjectProcessor.prototype);
-CreateProcessor.prototype.save = function() {};
+SaveProcessor.prototype.save = function(callback) {
+ callback(this.json);
+};
-function createProcessorFactory(store, type) {
+function saveProcessorFactory(store, type, includeId) {
return function(record) {
- return new CreateProcessor(record, store, type);
+ return new SaveProcessor(record, store, type, includeId);
};
}
@@ -212,8 +214,17 @@ DS.BasicAdapter = DS.Adapter.extend({
createRecord: function(store, type, record) {
var sync = type.sync;
+ sync.createRecord(record, saveProcessorFactory(store, type));
+ },
+
+ updateRecord: function(store, type, record) {
+ var sync = type.sync;
+ sync.updateRecord(record, saveProcessorFactory(store, type, true));
+ },
- sync.createRecord(record, createProcessorFactory(store, type));
+ deleteRecord: function(store, type, record) {
+ var sync = type.sync;
+ sync.deleteRecord(record, saveProcessorFactory(store, type, true));
}
});
@@ -42,9 +42,9 @@ DS.Model = Ember.Object.extend(Ember.Evented, LoadPromise, {
return store.serialize(this, options);
},
- toJSON: function() {
+ toJSON: function(options) {
var serializer = DS.JSONSerializer.create();
- return serializer.serialize(this);
+ return serializer.serialize(this, options);
},
didLoad: Ember.K,
@@ -139,6 +139,9 @@ DS.Model = Ember.Object.extend(Ember.Evented, LoadPromise, {
clearRelationships: function() {
this.eachRelationship(function(name, relationship) {
+ // if the relationship is unmaterialized, move on
+ if (this.cacheFor(name) === undefined) { return; }
+
if (relationship.kind === 'belongsTo') {
set(this, name, null);
} else if (relationship.kind === 'hasMany') {
@@ -98,8 +98,6 @@ DS.JSONTransforms = {
return dayOfWeek + ", " + dayOfMonth + " " + month + " " + utcYear + " " +
pad(utcHours) + ":" + pad(utcMinutes) + ":" + pad(utcSeconds) + " GMT";
- } else if (date === undefined) {
- return undefined;
} else {
return null;
}
@@ -48,6 +48,8 @@ module("Basic Adapter - Saving", {
});
test("After creating a record, calling `save` on it will save it using the BasicAdapter", function() {
+ expect(2);
+
Person.sync = {
createRecord: function(passedRecord, process) {
equal(passedRecord, person, "The person was passed through");
@@ -60,3 +62,40 @@ test("After creating a record, calling `save` on it will save it using the Basic
var person = Person.createRecord({ firstName: "Igor", lastName: "Terzic" });
person.save();
});
+
+test("After updating a record, calling `save` on it will save it using the BasicAdapter", function() {
+ expect(2);
+
+ Person.sync = {
+ updateRecord: function(passedRecord, process) {
+ equal(passedRecord, person, "The person was passed through");
+ process(passedRecord).save(function(json) {
+ deepEqual(json, { id: 1, firstName: "Igor", lastName: "Terzicsta", createdAt: null }, "The process method toJSON'ifies the record");
+ });
+ }
+ };
+
+ store.load(Person, { id: 1, firstName: "Igor", lastName: "Terzic" });
+ var person = Person.find(1);
+ person.set('lastName', "Terzicsta");
+
+ person.save();
+});
+
+test("After deleting a record, calling `save` on it will save it using the BasicAdapter", function() {
+ expect(2);
+
+ Person.sync = {
+ deleteRecord: function(passedRecord, process) {
+ equal(passedRecord, person, "The person was passed through");
+ process(passedRecord).save(function(json) {
+ deepEqual(json, { id: 1, firstName: "Igor", lastName: "Terzic", createdAt: null }, "The process method toJSON'ifies the record");
+ });
+ }
+ };
+
+ store.load(Person, { id: 1, firstName: "Igor", lastName: "Terzic" });
+ var person = Person.find(1);
+ person.deleteRecord();
+ person.save();
+});

0 comments on commit 53e14dd

Please sign in to comment.