【问题标题】:Ember action on itemController not workingitemController 上的 Ember 操作不起作用
【发布时间】:2014-09-08 06:05:22
【问题描述】:

这是我的控制器:

App.DesignPhotosController = Ember.ArrayController.extend({
    itemController: 'designPhoto'
});


App.DesignPhotoController = Ember.ObjectController.extend({
    needs: ['designPhotos'],
    toDelete: false,

    actions: {
        toggleDelete: function() {
            this.set('toDelete', !this.get('toDelete'));
        }
    }
});

还有我的模板:

{{#each}}
  <ul>
    <li>
      {{title}}
      {{#if toDelete}}
        <button class="restore" {{action "toggleDelete"}}>Restore</button>
      {{else}}
        <button class="delete" {{action "toggleDelete"}}>Delete</button>
      {{/if}}
    </li>
  </ul>
{{/each}}

但是,当我单击“删除”按钮时,我会收到一条消息:

Error: Nothing handled the action 'toggleDelete'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

据我所知,我这样做是正确的,我尝试通过以下各种组合来强制它:

  • target=this 添加到操作中

  • 添加 {{#each item in controller}} 和 target=item

  • 将操作更改为 {{action toggleDelete this}},带引号和不带引号

没有任何作用。

谁能指出我做错了什么?

【问题讨论】:

  • 似乎在这里工作emberjs.jsbin.com/xedol/1。代码看起来也不错,唯一可能发生的是模板的上下文控制器可能不是“designPhotos”(您可以在每个控制器之外记录控制器并检查是否是这种情况或不使用 {{log controller}} )
  • 神秘 - 刚刚再次打开我的代码尝试记录它,现在它可以工作了!也许是 Firefox 缓存中搞砸了。

标签: ember.js handlebars.js


【解决方案1】:

此行为的一个可能原因是a bug present in 1.13 (up until the very last 1.x release, 1.13.13),它破坏了应由项目控制器处理的操作。

如果您无法恢复到 1.12 版,则问题的第二个解决方法

将 {{#each}} 更改为 {{#each item in controller}} 并将 target=item 添加到操作中

是官方推荐的,并且适用于我的代码,虽然是第一个

target=this 添加到操作中(使用简单的{{#each}})

应该也可以。

(我怀疑 OP 将这些中的任何一个留在了代码中,并且没有注册它解决了问题,因为由于 Firefox 的非常持久的缓存,两者都早先出现了误报。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2016-09-25
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多