Access item controller in view

馋奶兔 提交于 2019-12-12 23:08:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!