Reputation: 3939
I have written a custom adapter for Ember-Data for use with Rhom API.
Here is the code:
'use strict';
DS.RhomAdapter = DS.Adapter.extend(Ember.Evented, {
extractVars: function(rhomRecord) {
return rhomRecord.vars();
},
objectToId: function(record) {
record["id"] = record.object;
return Ember.copy(record);
},
find: function(store, type, id) {
console.log('find');
var record = Rho.ORM.getModel(this.model).find(
'first',
{
conditions: {object: id}
}
);
var result = record.vars();
result["id"] = result.object;
return Ember.RSVP.resolve(result);
},
findAll: function(store, type) {
console.log('findALl');
var records = Rho.ORM.getModel(this.model).find('all');
var results = records.map(this.extractVars);
var results = results.map(this.objectToId);
var promise = new Ember.RSVP.Promise(function(resolve, reject){
// succeed
resolve(results);
// or reject
reject([]);
});
return promise;
},
createRecord: function(store, type, record) {
Rho.ORM.getModel(this.model).create(record.toJSON());
return Ember.RSVP.resolve();
},
updateRecord: function(store, type, record) {
console.log(record.get('id'));
var result = Rho.ORM.getModel(this.model).find('first', {conditions: {object: record.get('id')}});
result.updateAttributes(record.toJSON());
return Ember.RSVP.resolve();
},
deleteRecord: function(store, type, record) {
var result = Rho.ORM.getModel(this.model).find('first', {conditions: {object: record.get('id')}});
result.destroy();
return Ember.RSVP.resolve();
}
});
When a new record is created, the createRecord is called. But Ember does not know the id of the object that was created. Now when I try to modify that object in the view, updateRecord is called and that object is passed but that object does not have an id. So how do I update the backend if I dont know the ID?
Upvotes: 0
Views: 1031
Reputation: 1108
The promise returned by createRecord
should be resolved with the new record (including the id).
createRecord: function(store, type, record) {
var json = record.toJSON();
Rho.ORM.getModel(this.model).create(record.toJSON());
json.id = ....
return Ember.RSVP.resolve(json);
},
Upvotes: 1