Permalink
Browse files

Basic saving

  • Loading branch information...
1 parent 3c80d50 commit 36d2e2a07a82cc67baab186c36c38c405186ac12 tomhuda committed Mar 12, 2013
@@ -53,10 +53,6 @@ function ObjectProcessor(json, type, store) {
}
ObjectProcessor.prototype = {
- load: function() {
- this.store.load(this.type, {}, this.json);
- },
-
camelizeKeys: function() {
camelizeKeys(this.json);
return this;
@@ -73,9 +69,19 @@ ObjectProcessor.prototype = {
}
};
-function processorFactory(store, type) {
+function LoadObjectProcessor() {
+ ObjectProcessor.apply(this, arguments);
+}
+
+LoadObjectProcessor.prototype = Ember.create(ObjectProcessor.prototype);
+
+LoadObjectProcessor.prototype.load = function() {
+ this.store.load(this.type, {}, this.json);
+};
+
+function loadObjectProcessorFactory(store, type) {
return function(json) {
- return new ObjectProcessor(json, type, store);
+ return new LoadObjectProcessor(json, type, store);
};
}
@@ -151,14 +157,29 @@ function hasManyProcessorFactory(store, record, relationship) {
};
}
+function CreateProcessor(record, store, type) {
+ this.record = record;
+ ObjectProcessor.call(this, record.toJSON(), type, store);
+}
+
+CreateProcessor.prototype = Ember.create(ObjectProcessor.prototype);
+
+CreateProcessor.prototype.save = function() {};
+
+function createProcessorFactory(store, type) {
+ return function(record) {
+ return new CreateProcessor(record, store, type);
+ };
+}
+
DS.BasicAdapter = DS.Adapter.extend({
find: function(store, type, id) {
var sync = type.sync;
Ember.assert("You are trying to use the BasicAdapter to find id '" + id + "' of " + type + " but " + type + ".sync was not found", sync);
Ember.assert("The sync code on " + type + " does not implement find(), but you are trying to find id '" + id + "'.", sync.find);
- sync.find(id, processorFactory(store, type));
+ sync.find(id, loadObjectProcessorFactory(store, type));
},
findQuery: function(store, type, query, recordArray) {
@@ -187,6 +208,12 @@ DS.BasicAdapter = DS.Adapter.extend({
} else {
Ember.assert("You are trying to use the BasicAdapter to find the " + relationship.key + " has-many relationship, but " + record.constructor + ".sync did not implement findHasMany or find" + name + ".", false);
}
+ },
+
+ createRecord: function(store, type, record) {
+ var sync = type.sync;
+
+ sync.createRecord(record, createProcessorFactory(store, type));
}
});
@@ -42,6 +42,11 @@ DS.Model = Ember.Object.extend(Ember.Evented, LoadPromise, {
return store.serialize(this, options);
},
+ toJSON: function() {
+ var serializer = DS.JSONSerializer.create();
+ return serializer.serialize(this);
+ },
+
didLoad: Ember.K,
didReload: Ember.K,
didUpdate: Ember.K,
@@ -282,6 +287,12 @@ DS.Model = Ember.Object.extend(Ember.Evented, LoadPromise, {
becameInFlight: function() {
},
+ // FOR USE BY THE BASIC ADAPTER
+
+ save: function() {
+ this.get('store').scheduleSave(this);
+ },
+
// FOR USE DURING COMMIT PROCESS
adapterDidUpdateAttribute: function(attributeName, value) {
@@ -104,6 +104,8 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
// `isLoaded` and fires a `didLoad` event.
this.loadingRecordArrays = {};
+ this._recordsToSave = Ember.OrderedSet.create();
+
set(this, 'defaultTransaction', this.transaction());
},
@@ -914,6 +916,39 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
}
},
+ // .................
+ // . BASIC ADAPTER .
+ // .................
+
+ scheduleSave: function(record) {
+ this._recordsToSave.add(record);
+ Ember.run.once(this, 'flushSavedRecords');
+ },
+
+ flushSavedRecords: function() {
+ var created = Ember.OrderedSet.create();
+ var updated = Ember.OrderedSet.create();
+ var deleted = Ember.OrderedSet.create();
+
+ this._recordsToSave.forEach(function(record) {
+ if (get(record, 'isNew')) {
+ created.add(record);
+ } else if (get(record, 'isDeleted')) {
+ deleted.add(record);
+ } else {
+ updated.add(record);
+ }
+ });
+
+ this._recordsToSave.clear();
+
+ get(this, '_adapter').commit(this, {
+ created: created,
+ updated: updated,
+ deleted: deleted
+ });
+ },
+
// ..............
// . PERSISTING .
// ..............
@@ -0,0 +1,62 @@
+var get = Ember.get;
+
+var store, adapter, Person, PhoneNumber;
+module("Basic Adapter - Saving", {
+ setup: function() {
+ adapter = DS.BasicAdapter.create();
+ store = DS.Store.create({
+ adapter: adapter
+ });
+
+ var attr = DS.attr, hasMany = DS.hasMany, belongsTo = DS.belongsTo;
+ Person = DS.Model.extend({
+ firstName: attr('string'),
+ lastName: attr('string'),
+ createdAt: attr('date')
+ });
+
+ PhoneNumber = DS.Model.extend({
+ areaCode: attr('number'),
+ number: attr('number'),
+ person: belongsTo(Person)
+ });
+
+ Person.reopen({
+ phoneNumbers: hasMany(PhoneNumber)
+ });
+
+ DS.registerTransforms('test', {
+ date: {
+ serialize: function(value) {
+ return value.toString();
+ },
+
+ deserialize: function(string) {
+ return new Date(string);
+ }
+ }
+ });
+ },
+
+ teardown: function() {
+ Ember.run(function() {
+ DS.clearTransforms();
+ store.destroy();
+ adapter.destroy();
+ });
+ }
+});
+
+test("After creating a record, calling `save` on it will save it using the BasicAdapter", function() {
+ Person.sync = {
+ createRecord: function(passedRecord, process) {
+ equal(passedRecord, person, "The person was passed through");
+ process(passedRecord).save(function(json) {
+ deepEqual(json, { firstName: "Igor", lastName: "Terzic", createdAt: null }, "The process method toJSON'ifies the record");
+ });
+ }
+ };
+
+ var person = Person.createRecord({ firstName: "Igor", lastName: "Terzic" });
+ person.save();
+});
@@ -1,7 +1,7 @@
var get = Ember.get;
var store, adapter, Person, PhoneNumber;
-module("Basic Adapter", {
+module("Basic Adapter - Finding", {
setup: function() {
adapter = DS.BasicAdapter.create();
store = DS.Store.create({
@@ -113,6 +113,7 @@
DS.Model.reopen({
then: syncForTest(),
+ save: syncForTest(),
deleteRecord: syncForTest(),
dataDidChange: Ember.observer(syncForTest(), 'data'),
updateRecordArraysLater: syncForTest()

0 comments on commit 36d2e2a

Please sign in to comment.