Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data: Overriding Save method

Hello Ember Data World,

I have been studying custom adapters attempting to figure out how to override the save method.

From my understanding, it seems like you need to do something like this:

DS.RESTAdapter.extend({ 
   save: function() { return this._super();}
})

However, when I attempted to invoke the save operation on my model object using:

model.save()

The store gets invoked directly and not my adapter custom code.

Has anyone attempted to do this before?

I am able to call the find method using the following code in the same adapter

findQuery: function(store, type, query) {
        //debugger;
        console.log("findQuery: Custom adapter called!");

            return this.ajax(this.buildURL(type.typeKey), 'GET', { data: query });
}
like image 845
joker1979 Avatar asked May 20 '14 18:05

joker1979


1 Answers

save is actually defined on the record, not the adapter. If you felt like overriding it you would do so in your model definition.

Here's an example of that:

App.Color = DS.Model.extend({
    color: DS.attr(),

    save: function(){
      alert('save');
    }
});

http://emberjs.jsbin.com/OxIDiVU/497/edit

Now if you felt like overriding what save eventually calls on the adapter you would need to update one of three methods, createRecord, updateRecord, deleteRecord. They are pretty self explanatory in what they each should do when save is called. You would then follow a pattern such as you did above:

App.ApplicationAdapter= DS.RESTAdapter.extend({
  updateRecord: function(){
    alert('update record');
  }
});

http://emberjs.jsbin.com/OxIDiVU/498/edit

like image 88
Kingpin2k Avatar answered Nov 01 '22 06:11

Kingpin2k