【问题标题】:Handlebar.helper's view cannot use actions from controllerHandlebar.helper 的视图不能使用来自控制器的操作
【发布时间】:2014-12-04 18:20:39
【问题描述】:

如何从使用车把助手的视图中调用控制器内部的动作(不确定车把助手是否与获取控制器相关,但我还是想提一下)?

VpcYeoman.EditRecordCategoryView = Ember.TextField.extend({
   //This is where i call the action from any controller
  click: function() { // might be a property or some other action
    this.get('controller').send('controllerActionName');  
  }
});

Ember.Handlebars.helper('edit-recordCategory', VpcYeoman.EditRecordCategoryView);

我会在该视图中做什么来告诉控制器我希望它激活其中一个动作?

编辑 1:

我将this.get('controller').send('controllerActionName'); 添加到click:function(){}, 我在该代码行收到错误Uncaught TypeError: undefined is not a function

如果我将this.get('controller').send('controllerActionName'); 添加到 didInsertElement: function(),我根本不会收到任何错误。

编辑 2:

车把助手位于使用名为recordType 的对象控制器的列表中。 “recordType”是我希望从车把帮助的点击事件中调用的那个动作。不过

在record_types.hbs中

{{#each itemController="recordType"}}
  {{#unless isEditing}}
     {{categoryName}}  
  {{/unless}}
  {{#if isEditing}}
    {{edit-recordCategory class="edit" value=category_name focus-out="acceptChanges" insert-newline="acceptChanges"}} //This is the handlebar helper
  {{/if}}
{{/each}}

我们可以假设记录中的isEditing 属性是true,因为如果它是false,则车把助手将不可见。

实际上,我项目控制器中的操作是为了切换该属性isEditing

【问题讨论】:

    标签: javascript model-view-controller ember.js handlebars.js


    【解决方案1】:

    要从其视图调用控制器操作,您可以这样做:

    VpcYeoman.EditRecordCategoryView = Ember.TextField.extend({
      //This is where i call the action from any controller
      click: function() { // might be a property or some other action
        this.get('controller').send('controllerActionName');  
      }
    });
    

    【讨论】:

    • 我收到错误Uncaught TypeError: undefined is not a function 这么久。会不会不知道里面的控制器是什么?
    • 并非如此,视图将始终有一个控制器,因为它们默认为呈现它们的上下文。你怎么渲染?你的模板是什么样的?
    • 好的,因为对象控制器设置在 {{#each}} 循环中(也是我想要的操作所在的位置)。我为你更新了问题(编辑 2)
    • 看起来不错,它应该可以工作,假设呈现record_types.hbs 的控制器是Ember.ArrayController
    • 有什么方法可以使用 this.get('objectcontroller') 或 this.get('arraycontroller')?
    猜你喜欢
    • 2015-11-19
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多