0

是否可以通过车把将数据链接到 Ember.view ?像带有 valueBindig 的 Ember.Select。

我的尝试:

{{#each models}}
<li>{{id}}</li>
{{/each}}

// -> 1,2,3 (this works fine)

{{view App.TimelineView valueBinding="models.id"}} //this doesn't work



App.TimelineView = Ember.View.extend({

    tagName: 'div',
    attributeBindings: ["value"],
    value: null,

    didInsertElement: function(){ 

    console.log(this.value) 
    ...
    })

控制台日志:---> null

但我需要 [1,2,3]

4

2 回答 2

0

假设models是一个数组,models.id正在访问id数组本身的属性,而不是每个元素的属性。您可能想要的是将模型映射到 id。

在定义的控制器上定义以下属性models

modelIds: Ember.computed.mapBy('models', 'id')

然后modelIds在模板中使用而不是models.id.

此外,您应该访问 properties this.get-style,而不是直接访问(didInsertElement例如)。

于 2013-09-22T11:14:30.107 回答
0

您没有value正确访问。正确的做法如下。那里还有一些我修复的错误。

App.TimelineView = Ember.View.extend({
    tagName: 'div',
    didInsertElement: function() { 
       console.log(this.get('value'));
    },
)};

{{#each models}}
   {{view App.TimelineView valueBinding="id"}} 
{{/each}}

循环中的上下文是 this,它代表迭代的当前元素,所以你想要 'this.id' 或简单的 'id'。

补充:如果你想id一次获得所有 s 的列表作为一个数组(而不是逐项),你可以执行以下操作:

App.MyController = Ember.ArrayController.extend({
   allIds: function() {
      var ids = this.get('content').getEach('id').reduce(function(accum, item) {
         return accum.push(item);
      }, []);
      return ids;
   }.property('content.@each.id');
}); 
于 2013-09-22T17:31:06.813 回答