访问项目控制器 [英] Access item controller in view

查看:95
本文介绍了访问项目控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

App.AreasController = Ember.ArrayController.extend({
  itemController: 'area'
});

// In my project route:
setupController: function(controller, model) {
  this.controllerFor('areas').set('content', model.areas);
}

在我看来,我正在编辑我的应用程序。区域,并且想从我的视图中调用给定的区域的控件方法(itemController)。我该怎么办?

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?

如果我尝试访问控制器 area.get('controller')(其中区域表示 model.areas 中的一个项目),它返回 unknown

If I try to access the controller area.get('controller') (where area represents one item from model.areas), it returns unknown.

如果我做 area.send('save'),我会收到错误:尝试在< App.Area上处理事件'save':ember1013:5230a2ee945f3b718a00006e>而在状态root.loaded.saved中。有什么想法可以解决这个问题?

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?

推荐答案

我想你会想要这样的:

{{#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'));
    }
});

这是一个总体思路的JSBin: http://jsbin.com/ucanam/1045/edit

Here's a JSBin of the general idea : http://jsbin.com/ucanam/1045/edit

在你看来, itemController,而不是项目本身。由于控制器位于视图和项目之间,因此您不需要尝试从控件中获取控件。所以,你直接在itemController上调用方法。

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.

这篇关于访问项目控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆