blueFast
blueFast

Reputation: 44381

How to manually update an ArrayController

In my Application I have the following rawNodes property, which I am using as an application-wide cache:

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();
        }
    },
    ...
});

In some of my controllers I am modifying data which should also be updated in this generic cache. I would like to implement a couple of functions, to update a given node, and to delete a given node. Something like:

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

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

But I do not really know how to work with an ArrayController, not even if those operations are at all possible. I see no examples of this kind of procedures in the ArrayController documentation. Could somebody offer an example, or point me in the right direction?

Upvotes: 1

Views: 73

Answers (1)

James
James

Reputation: 4737

Rather than using a rawNodes property, I think it might be more useful to maintain a Node model and a NodesController. Assign the model property with setupController so you can be sure that nodes are always fetched.

Since this is an application-wide cache, use needs in ApplicationController so it can delegate to its methods.

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() {
    // ...
  }
});

Upvotes: 1

Related Questions