Ember.js计算属性与数组属性';s元素
我希望类中有一个计算属性,它依赖于同一类的属性(字符串数组)。我希望此计算属性仅依赖于数组的特定元素 实际上,我也不介意它是否依赖于数组的所有元素 我将余烬定义为:Ember.js计算属性与数组属性';s元素,ember.js,Ember.js,我希望类中有一个计算属性,它依赖于同一类的属性(字符串数组)。我希望此计算属性仅依赖于数组的特定元素 实际上,我也不介意它是否依赖于数组的所有元素 我将余烬定义为: var UpdateController = Ember.ArrayController.extend({ PANE_1: 0, PANE_2: 1, PANE_3: 2, init: function() { this._super();
var UpdateController = Ember.ArrayController.extend({
PANE_1: 0,
PANE_2: 1,
PANE_3: 2,
init: function() {
this._super();
this.set('paneClass', ["center", "right", "right"]);
},
channelsPaneClass: function() {
return this.get('paneClass')[this.get('PANE_1')];
}.property('paneClass.@each'),
}
此计算属性正在模板中使用,如下所示:
<div {{bind-attr class=":sf-seg-pane channelsPaneClass"}}></div>
我原以为html会变成:
<div class="sf-seg-pane xxx"></div>
但事实并非如此
我还试过什么:
.property('paneClass')
{{bind attr class=“:sf seg pane paneClass[0]”}
您需要使用setter,您实际上是在从ember下更改数据。我建议您使用
paneClass.[]
而不是paneClass.@each
因为paneClass.@each
用于观察数组中某个对象的某些属性。还可以在属性中添加窗格_1
,因为这是一个依赖键
var UpdateController = Ember.ArrayController.extend({
PANE_1: 0,
PANE_2: 1,
PANE_3: 2,
init: function() {
this._super();
this.set('paneClass', ["center", "right", "right"]);
},
channelsPaneClass: function() {
return this.get('paneClass')[this.get('pane1')];
}.property('paneClass.[]', 'PANE_1'),
}
要通知观察者数组已更改,您需要使用array.insertAt(index,content)
而不是array[index]=content
。就你而言:
this.get('paneClass').insertAt(this.get('PANE_1'), "xxx");
如何为数组元素使用setter?如果它是一个对象,我会使用这个.set('paneClass','xxx')。但是paneClass
是一个数组,我想将它的第0个元素设置为“xxx”。工作起来很有魅力!谢谢你抽出时间。如果你不介意的话,这有没有记录在案。我在谷歌上搜索了几个小时。最后,我使用了一个涉及如下所述对象的变通方法:不客气。关于insertAt
我只知道一个地方。对不起,我指的是paneClass。[]
语法。没问题。是@each的文档吗
var UpdateController = Ember.ArrayController.extend({
PANE_1: 0,
PANE_2: 1,
PANE_3: 2,
init: function() {
this._super();
this.set('paneClass', ["center", "right", "right"]);
},
channelsPaneClass: function() {
return this.get('paneClass')[this.get('pane1')];
}.property('paneClass.[]', 'PANE_1'),
}
this.get('paneClass').insertAt(this.get('PANE_1'), "xxx");