如何在ember视图中使用像location.rating [issue.id]这样的动态数组值

时间:2012-05-02 23:54:53

标签: ember.js

我只是想显示只选择一个数组,如果它们包含在另一个数组中但似乎我不能这样做,如果你知道我应该怎么做,或者你有一个如何做的建议我会很高兴听到它。

这是我的车把视图,

    {{#each App.User.issues}}
        {{#view App.LocationListItemIssueView issueBinding="this" locationBinding="parentView.location"}}
            {{#if location.rating[issue.id] != null}}
                {{location.rating[issue.id].value}}
           {{else}}         
                {{issue.name}} Not Rated    
           {{/if}}      
           {{issue.name}}   
        {{/view}} 
    {{/each}}   

我得到了他的错误

Uncaught Error: Parse error on line 1:
...                 {{#if location.rating[issue.id] != 
-----------------------^
Expecting 'ID'

2 个答案:

答案 0 :(得分:1)

如果不能像if()那样工作..它不需要一个完整的语句并对其进行评估,而是采用一个转换为布尔值的属性的路径。

如果要在集合视图中显示两个数组的交集,则应创建一个arrayController,并使该arrayController的内容成为计算数组交集的计算属性。然后,将您的collectionview绑定到该数组控制器。

这看起来像是:

MyApp.intersectionController = Em.ArrayController.create({
  arrayOneBinding: 'MyApp.firstArray',
  arrayTwoBinding: 'MyApp.secondArray',
  content: function() {
    ... create and return an array which is the intersection of both arrays ...
  }.property('arrayOne.@each', 'arrayTwo.@each').cacheable()
});

如果要渲染数组中的所有项目,但是基于另一个数组以某种方式更改模板,则可以在itemViewClass中执行以下操作:

rating: function() {
    return this.getPath('location.rating')[this.getPath(content.id)];
}.property('content', 'location').cacheable();

然后在你看来:

{{#if rating}}
  {{rating}}
{{else}}
  {{content.name}} not reated
{{/if}}

答案 1 :(得分:0)

这也可以通过Handlebars助手来解决。

Handlebars.registerHelper('compare', function(value, options){
    //This would be tweaked to fit your needs. 
    //I'm checking that a passed in value matches the bound value.
    return this.get('content_type') === value ? options.fn(this) : options.inverse(this);
})

http://handlebarsjs.com/expressions.html