Javascript 模型不';t在Emberjs中删除记录后更新

Javascript 模型不';t在Emberjs中删除记录后更新,javascript,ajax,ember.js,Javascript,Ajax,Ember.js,实际上,我在我的Ember应用程序中使用Ajax而不是Ember数据来执行CRUD操作 场景是每当我删除记录时,模型都不会更新。我有一个项目列表,每个项目前面都有一个“删除”按钮 以下是与删除关联的操作: deleteItem: function(item){ //function for delete action var id = item.get('id'); $.ajax({ type : "POST",

实际上,我在我的Ember应用程序中使用Ajax而不是Ember数据来执行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')

我用一些注释更新了你的代码。我希望有帮助

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

好啊成功被触发了?成功被触发了,我得到“记录删除”提示,但是模型没有更新。我更新了答案,也许现在可以工作了。只需将计算属性中的
model
更改为
model.length
,实际上我还发现了一些其他内容,它也不
console.log('success')
。触发成功处理程序时,该项将被删除。您可以进行测试,并将成功代码处理程序放在ajax调用之外。因此,它将始终被执行。我们可以看到客户端是否正常,问题是否出在服务器上。我要睡觉了,明天再试试。成功被触发了?成功被触发了,我得到“记录删除”提示,但是模型没有更新。我更新了答案,也许现在可以工作了。只需将计算属性中的
model
更改为
model.length
,实际上我还发现了一些其他内容,它也不
console.log('success')
。触发成功处理程序时,该项将被删除。您可以进行测试,并将成功代码处理程序放在ajax调用之外。因此,它将始终被执行。我们可以看到客户端是否正常,问题是否出在服务器上。我会睡觉,明天再多尝试。