2012-05-18 32 views
2

我有一个Ember.js应用如下(借用this article上andyMatthews.net):Ember.js传递参数从视图控制器功能

检视:

<script type="text/x-handlebars" > 
    {{view App.UserTextField}} 
    {{#each App.recentUsersArray.reverse tagName="ul"}} 
     <li>{{this}}</li> 
    {{/each}} 
</script>​ 

应用程序:

App = Ember.Application.create(); 

App.UserTextField = Ember.TextField.extend({ 
    insertNewline: function(){ 
     var term = this.get('value'); 
     App.recentUsersArray.pushObject(term); 
    } 
}); 

App.recentUsersArray = Em.ArrayController.create({ 
    content: ['noelle', 'evan', 'mason'], 
    reverse: function(){ 
     return this.get('content').toArray().reverse(); 
    }.property('[email protected]').cacheable(), 
});​ 

但我想从视图传递参数到App.recentUsersArray.reverse(例如,设置要返回的反转项目的数量)。我碰到一个问题,因为虽然你可以接受控制器功能上的参数,但它看起来并不像你可以从视图中传递它。我想象这样的事情,如果你想在参数3传递到反向功能的控制器上:

{{#each App.recentUsersArray.reverse(3) tagName="ul"}} 
    <li>{{this}}</li> 
{{/each}} 

但它抛出一个错误(未被捕获的错误:解析错误)...想法?最终目标是创建三个<ul>,每个具有不同数量的元素而不是为每个长度创建不同的函数(即,App.recentUsersArray.reverseThree(),App.recentUsersArray.reverseFive()等)。我也不想制作三个不同的控制器,因为每个<ul>都应该使用相同的数据源,只是切片不同。

+0

你必须阅读有关性理解为什么之前如何计算工作你要求的是没有意义的。 –

回答

2

是的,Ember可以被看作是一个奇怪的范例。也许尝试:

App.recentUsersArray = Em.ArrayController.create({ 
    recentCount: 3 
    content: ['noelle', 'evan', 'mason'], 
    reverse: function(){ 
     var max = this.get('recentCount'); 
     return this.get('content').reverse().slice(0, max) 
    }.property('[email protected]', 'recentCount').cacheable(), 
});​ 

//some event handler 
App.recentUsersArray.set('recentCount', count) 

我不认为toArray()是必要的,再加上文档说,它并不能保证元素的顺序返回: http://docs.emberjs.com/#doc=Ember.Enumerable&method=.toArray&src=false

相关问题