0

I have a modal view which has a form. When I try to save the model, a REST call is made and I need to close the modal view after that..

self.myModel.url = "api/myClass/";      
self.myModel.save({
    success: function() {
        self.trigger("afterSave");
        self.removeModal();
    }
}).fail(function() {
    $('#save', self.el).button('reset');
});

Now my REST API code is executed fine...I have debugged on the last return statement from REST resource.. But my JS code does not go inside the success callback function.

Am I doing something wrong with the JS syntax?

copenndthagen
  • 49,230
  • 102
  • 290
  • 442

2 Answers2

1

So here is your problem:

self.myModel.save({
    success: function() {
        self.trigger("afterSave");
        self.removeModal();
    }
})

.save() method expects the first parameter to be the data. And the second object should contain options. If you don't have any data to pass, you should modify the above call to this:

self.myModel.save({},{
        success: function() {
            self.trigger("afterSave");
            self.removeModal();
        }
    });

You should be all set this way! Suggest you to check the documentation : Backbone Docs

beNerd
  • 3,314
  • 6
  • 54
  • 92
0

the first param for save() should be the model data and the second is the callbacks

self.myModel.url = "api/myClass/";      
self.myModel.save({
     "name":"Bob"
   },{
     success: function() {
        self.trigger("afterSave");
        self.removeModal();
     }
   }).fail(function() {
   $('#save', self.el).button('reset');
});
Qusai Jouda
  • 1,048
  • 8
  • 12