'Backbone.js model's destroy method does not trigger success or error event
I am beginning to learn Backbone.js and I started with this boilerplate and made an example by loading JSON data from a static file on disk and display it in an html table.
Then I tried to bind an event on a button which is supposed to delete an element from the collection, then from the DOM. The thing works fine, the click triggers the destroy method, the remove event is triggered on the collection, but nothing comes out of the success or error callbacks of destroy
Anybody have a clue ?
The model :
define([
'underscore',
'backbone'
], function(_, Backbone) {
var memberModel = Backbone.Model.extend({
url: "/members.json",
defaults: {
email: "",
firstname: "",
lastname: "",
gender: "",
language: "",
consent: false,
guid: "",
creationDate: ""
},
initialize: function(){
}
});
return memberModel;
});
The view :
define([
'jquery',
'underscore',
'backbone',
'mustache',
'collections/members',
'text!templates/members/page.html'
], function($, _, Backbone, Mustache, membersCollection, membersPageTemplate){
var membersPage = Backbone.View.extend({
el: '.page',
initialize: function(){
this.members = new membersCollection();
this.members.on('remove', function(){
// works fine
$('.members-body tr').first().remove();
console.log('removed from collection');
});
},
render: function () {
var that = this;
this.members.fetch({success: function(){
var wrappedMembers = {"members" : that.members.toJSON()};
that.$el.html(Mustache.render(membersPageTemplate, wrappedMembers));
$('#delete-member').click(function(){
that.members.at(0).destroy({
// prints nothing!!!
success: function(){ console.log('sucess'); },
error: function(){ console.log('error'); }
});
});
}});
}
});
return membersPage;
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
