Ember.js 如何手动更新ArrayController

Ember.js 如何手动更新ArrayController,ember.js,ember-data,Ember.js,Ember Data,在我的应用程序中,我有以下rawNodes属性,我将其用作应用程序范围的缓存: var App = Ember.Application.createWithMixins({ ... /** The rawNodes property is a nodes deposit to be used to populate combo boxes etc. **/ rawNodes: null, getNodes: function (

在我的应用程序中,我有以下
rawNodes
属性,我将其用作应用程序范围的缓存:

var App = Ember.Application.createWithMixins({
    ...
    /**
      The rawNodes property is a nodes deposit to be used
      to populate combo boxes etc.

    **/
    rawNodes: null,

    getNodes: function () {
        if (!this.rawNodes) {
            this.rawNodes = this.Node.find();
        }
    },
    ...
});
在我的一些控制器中,我正在修改数据,这些数据也应该在这个通用缓存中更新。我想实现两个函数,更新给定节点和删除给定节点。比如:

updateNode: function(node_id, node) {
    this.rawNodes.update(node_id, node);
},

deleteNode: function(node_id) {
    this.rawNodes.delete(node_id);
}

但我真的不知道如何使用ArrayController,即使这些操作是完全可能的。我在报告中没有看到这种程序的例子。有人能举个例子,或者给我指出正确的方向吗?

与其使用
rawNodes
属性,不如 维护
节点
模型和
节点控制器
。分配
模型
属性 使用
setupController
,您可以确保始终获取节点

由于这是一个应用程序范围的缓存,请在
ApplicationController
中使用
needs
,以便它可以委托给自己的方法

App.ApplicationRoute = Em.Route.extend({
  setupController: function() {
    this.controllerFor("nodes").set("model", App.Node.find());
  }
});

App.ApplicationController = Em.Controller.extend({
  needs: "nodes",
});

App.NodesController = Em.ArrayController.extend({
  getNodes: function() {
    // ...
  }
});

App.NodeController = Em.ObjectController.extend({
  updateNode: function() {
    // ...
  },

  deleteNode: function() {
    // ...
  }
});

我认为与其使用
rawNodes
属性,不如使用 维护
节点
模型和
节点控制器
。分配
模型
属性 使用
setupController
,您可以确保始终获取节点

由于这是一个应用程序范围的缓存,请在
ApplicationController
中使用
needs
,以便它可以委托给自己的方法

App.ApplicationRoute = Em.Route.extend({
  setupController: function() {
    this.controllerFor("nodes").set("model", App.Node.find());
  }
});

App.ApplicationController = Em.Controller.extend({
  needs: "nodes",
});

App.NodesController = Em.ArrayController.extend({
  getNodes: function() {
    // ...
  }
});

App.NodeController = Em.ObjectController.extend({
  updateNode: function() {
    // ...
  },

  deleteNode: function() {
    // ...
  }
});

这是有道理的,但仍然是:如何实现
updateNode
deleteNode
?这是一个很好的观点。我想这两种方法应该放在一个
节点控制器中(答案更新),用于对单个资源执行操作。就实际更新和删除而言,这取决于模型。例如,如果使用的是
ember数据
,则需要修改属性,然后调用
node.store.commit()
node.deleteRecord()
。如果您的模型是一个普通的
Ember.Object
,那么您必须自己使用
$.ajax
或任何您需要用于应用程序的方法来实现这些方法。这是有道理的,但仍然是:我如何实现
updateNode
deleteNode
?这是一个很好的观点。我想这两种方法应该放在一个
节点控制器中(答案更新),用于对单个资源执行操作。就实际更新和删除而言,这取决于模型。例如,如果使用的是
ember数据
,则需要修改属性,然后调用
node.store.commit()
node.deleteRecord()
。如果您的模型是一个普通的
Ember.Object
,那么您必须自己使用
$.ajax
或任何您需要用于应用程序的方法来实现这些方法。