2014-09-30 51 views
1

有沒有辦法觸發itemController中定義的動作。當我用<button {{action 'inItem' }}>觸發此動作時,它會拋出未捕獲錯誤:沒有處理事件'inItem'。。如果我使用<button {{action 'inItem' target=item}}>設置動作的目標,則會拋出錯誤未捕獲的TypeError:無法讀取未定義的屬性'apply'如何觸發在it​​emController中定義的Emberjs動作

如何觸發itemController中定義的操作。

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

回答

1

你的控制器應延長ArrayController,不Array

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

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

+0

謝謝你的幫忙。 – brg 2014-09-30 21:11:13

+0

如何使parentController的路由中定義的** show action **調用itemController中定義的** inItem action **。謝謝 – brg 2014-09-30 21:27:39

+0

我不確定我是否理解你的問題,但是這看起來更像是一個更可能的工作流程:http://emberjs.jsbin.com/nabuwo/3/edit – Kingpin2k 2014-09-30 21:30:11

相關問題