I am starting to learn Backbone.js and I started with this template and made an example by loading the JSON data from a static file to disk and displaying it in the html table.
Then I tried to bind an event on a button that should remove an item from the collection, and then from the DOM. Everything works fine, a click fires the destroy method, the remove event fires in the collection, but nothing comes out of the success or error destroy callbacks
Does anyone have a key?
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; });
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; });
Maxiwheat
source share