如何触发itemController中定义的Emberjs动作

时间:2014-09-30 19:42:25

标签: javascript ember.js ember-controllers

是否有办法触发itemController中定义的操作。当我使用<button {{action 'inItem' }}>触发操作时,它会抛出未捕获错误:没有处理事件&#39; inItem&#39;。。如果我使用<button {{action 'inItem' target=item}}>设置操作的目标,则会抛出错误,未捕获的TypeError:无法读取属性&#39; apply&#39;未定义的

如何触发itemController中定义的操作。

&#13;
&#13;
App = Ember.Application.create({
  LOG_TRANSITIONS: true
});

App.Router.map(function(){
   this.resource('posts');           
               
 });

App.PostsRoute = Ember.Route.extend({
  model: function(){
    return [
      {title: 'success'},
      {title: 'winning'},
      {title: 'breakthrough'}
    ];
  }
  
});

App.PostsController = Ember.Array.extend({
  itemController: 'post'
});
App.PostController = Ember.ObjectController.extend({
  isSelected: false,
  
  actions: {
    inItem: function(){
       this.set('isSelected', true);
      //alert('hi');
    }
  }
  
});
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="http://builds.emberjs.com.s3.amazonaws.com/tags/v1.0.0/ember.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <script type='text/x-handlebars' id='application'>
    {{#link-to 'posts'}} All posts {{/link-to}}
    {{outlet}}
   </script>
  
   <script type='text/x-handlebars' data-template-name='posts'>
   
     <br>
     {{#each item in controller }}
         
       {{#if item.isSelected}}
         {{item.title}}
       {{/if}}
       
       <br>
       
       <button {{action 'inItem' target=item}} {{bind-attr class="isSelected"}}> call itemcontroller action </button>


     {{/each}}
     
     
  </script>
    
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您的控制器应该延伸ArrayController,而不是Array

App.PostsController = Ember.ArrayController.extend({
  itemController: 'post'
});

http://emberjs.jsbin.com/nabuwo/1/edit