简体   繁体   中英

Model doesn't update after record delete in Emberjs

I am actually using Ajax instead of Ember-Data to do CRUD operations in my Ember app.

Scenario is whenever I am deleting a record, the model doesn't update. I have a list of projects and each project has a 'Delete' button in front of it.

Here's the action associated with Delete:

deleteItem: function(item){ //function for delete action
            var id = item.get('id');
            $.ajax({
                type : "POST",
                url : "http://pioneerdev.us/users/deleteProject/" + id,
                dataType : "json",
                success : function(data) {
                    console.log('success');
                }
            });
            alertify.success('Record Deleted!');
            window.setTimeout(function(){location.reload()},3000)
        }

As you can see I am using location.reload to manually reload the model. If anyone's interested he can look at the full source at my GitHub repo

Is there any better way to do that?

I updated your code with some comments. I hope it helps.

actions: {
  deleteItem: function(item){
    var id = item.get('id');
    var controller = this;
    $.ajax({
      type : "POST",
      url : "http://pioneerdev.us/users/deleteProject/" + id,
      dataType : "json",
      // use the success callback to safe notify the user when the record is deleted
      // in your current implementation the alert is displayed even if an error occurs in the server.
      success : function(data) {          
        // no need to location.reload, the removeObject will remove that item
        controller.removeObject(item);                    
        alertify.success('Record Deleted!');
      }
    });  
  }
},
filteredContent : function() {
  var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

  return this.get('arrangedContent').filter(function(item) {
    return regex.test(item.projectname);
  });
  // instead of model use model.length so your template will update when some item is added or removed
}.property('searchText', 'model.length')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM