模型没有'在Emberjs中删除记录后不会更新

Model doesn't update after record delete in Emberjs

本文关键字:记录 删除 更新 Emberjs 模型      更新时间:2023-09-26

实际上,我在Ember应用程序中使用Ajax而不是Ember Data来执行CRUD操作。

场景是每当我删除记录时,模型都不会更新。我有一个项目列表,每个项目前面都有一个"删除"按钮

以下是与删除相关的操作:

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)
        }

如您所见,我正在使用location.reload手动重新加载模型。如果有人感兴趣,他可以在我的GitHub回购上查看完整的来源

有更好的方法吗?

我用一些注释更新了您的代码。我希望它能有所帮助。

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')