【问题标题】:Ember.js Action error: "Nothing handled the event"Ember.js 操作错误:“没有处理事件”
【发布时间】:2013-05-14 18:49:06
【问题描述】:

我正在开发我的第一个 Ember.js 应用程序,虽然我有一个模板加载,但当我尝试调用我在控制器中定义的操作时,我收到一个错误:“未捕获的错误:没有处理事件'showNew'。”我不确定我是否错误地设置了路由和控制器,或者我是否遗漏了其他东西。

./router.js:

Seanchai.Router.map(function(){
  this.resource("stories", function(){
    this.route('new');
  });
});

Seanchai.StoriesRoute = Ember.Route.extend({
  model: function(){
    Seanchai.Story.find();
  }
});


Seanchai.Router.reopen({
  location: 'history'
});

./controllers/stories_controller.js:

Seanchai.StoriesController = Ember.ArrayController.extend({    

  showNew: function() {
    this.set('isNewVisible', true);
  }
});

./templates/stories/index.hbs:

<table>
  <thead>
  <tr>
    <th>ID</th>
    <th>Name</th>
  </tr>
  </thead>
  <tbody>
    {{#each stories}}
      {{view Seanchai.ShowStoryView storyBinding="this"}}
    {{/each}}
    {{#if isNewVisible}}
      <tr>
        <td>*</td>
        <td>
          Test
        </td>
      </tr>
    {{/if}}
    </tbody>
</table>
<div class="commands">
  <a href="#" {{action showNew}}>New Story</a>
</div>

如果我将操作移动到路由器中,就像这样,它可以工作,但根据文档,我应该能够在控制器中执行此操作。

更新 ./router.js:

Seanchai.Router.map(function(){
  this.resource("stories", function(){
    this.route('new');
  });
});

Seanchai.StoriesRoute = Ember.Route.extend({
  model: function(){
    Seanchai.Story.find();
  },
  events: {
    showNew: function() {
      this.set('isNewVisible', true);
    }
  }
});


Seanchai.Router.reopen({
  location: 'history'
});

我显然遗漏了一些东西,但我不确定是什么。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    我猜你的showNew 事件没有在控制器上触发,因为你有一个stories.index 模板,所以你应该挂钩到应该是StoriesIndexController 的对应控制器:

    Seanchai.StoriesIndexController = Ember.ArrayController.extend({    
      showNew: function() {
        this.set('isNewVisible', true);
      }
    });
    

    希望对你有帮助

    【讨论】:

    • 做到了!太感谢了。我认为,在没有 StoriesIndexController 的情况下,它将汇总到 StoriesController 中的操作。所以,我需要一个 stories.hbs 模板作为使用 StoriesController 渲染的内容,否则我会使用与该模板操作匹配的控制器?只是想确保我理解模型。
    • 基本上,当您在路由器上定义了this.resource('stories') 之类的资源时,您还需要一个额外的index 路由才能连接,如果您只定义一个@,情况并非如此987654328@,请参见resources emberjs.com/guides/routing/defining-your-routes
    【解决方案2】:

    我认为应该是:

    Ember.ObjectController.extend({    
    
      showNew: function() {
        this.set('isNewVisible', true);
      }
    
    });
    

    代替:

    Ember.ArrayController.extend({    
    
      showNew: function() {
        this.set('isNewVisible', true);
      }
    });
    

    本指南可能会有所帮助 - Ember.js - Templates

    【讨论】:

    • 我很确定我已经试了一下,但只是为了确保我做了这个改变,但我得到了同样的错误。我认为 ArrayController 是我想根据它与模型的交互方式来扩展的那个,但它似乎不会以任何方式影响这个问题。谢谢!
    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多