问题
I have the following setup:
App.AreasController = Ember.ArrayController.extend({
itemController: 'area'
});
// In my project route:
setupController: function(controller, model) {
this.controllerFor('areas').set('content', model.areas);
}
In my view I am now editing my App.Areas, and would like to invoke a controller method (of the itemController) for a given area from my view. How do I go about that?
If I try to access the controller area.get('controller') (where area represents one item from model.areas), it returns unknown.
If I do area.send('save'), I get an error along the lines of Error: Attempted to handle event 'save' on <App.Area:ember1013:5230a2ee945f3b718a00006e> while in state root.loaded.saved. Any ideas on how I could solve this?
回答1:
I think you're going to want something like this:
{{#each}}
<li {{action toggleEdit}}>
<!-- Other stuff goes here -->
</li>
{{/each}}
App.AreaController = Ember.ObjectController.extend({
isEditing : false,
toggleEdit : function(){
this.set('isEditing', !this.get('isEditing'));
}
});
Here's a JSBin of the general idea : http://jsbin.com/ucanam/1045/edit
Inside of your view the "next level up" is the itemController, not the item itself. You don't really need to try to get the controller from the item because the controller is sitting in between the view and the item. So, you just call methods directly on the itemController.
来源:https://stackoverflow.com/questions/18771141/access-item-controller-in-view