each可以观察数组中的数组吗

each可以观察数组中的数组吗

本文介绍了使用@each可以观察数组中的数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一系列事物的控制器。在这些事情的每一个之内是一系列的子。我想创建一个计算属性,其中包含所有内容中的所有子代(一个扁平化的子集)。



我的计算属性取决于的东西。@ each.subthings.length 。我发现如果我设置某个东西的 subthings 属性,我的计算属性将被更新。但是,如果我调用 pushObjects 将新数据添加到我现有 subthings 数组中,属性不更新。



我创建了一个进行演示。代码如下:

  App = Em.Application.create({}); 

App.controller = Em.Object.create({
things:[
Em.Object.create({subthings:[]}),
Em。 Object.create({subthings:[]}),
Em.Object.create({subthings:[]})
],

allSubThings:function(){
var things = this.get('things');
var results = [];
things.forEach(function(thing){
results.pushObjects(thing.get( 'subthings'));
});
返回结果;
} .property('things。@ each.subthings.length')。cacheable()
});


setTimeout(function(){
var things = App.controller.get('things');
//这样做:
东西.objectAt(0).set('subthings',[1,2,3]);
},1000);

setTimeout(function(){
var things = App.controller.get('things');
//这不是:
things.objectAt 1).get('subthings')。pushObjects([1,2,3]);
},2000);


谢谢!

解决方案

将另一个 @each 添加到属性列表 .property('things。@ each.subthings。@ each.length')




I have a controller that contains an array of "things". Within each of these things is an array of "subthings". I'd like to create a computed property that contains all subthings in all things (a flattened array of subthings).

My computed property depends on [email protected]. I find that if I set the subthings property of a thing, my computed property is updated. However if I call pushObjects to add new data to my existing subthings array, my computed property does not update.

I've created a jsfiddle to demonstrate. The code is as follows:

App = Em.Application.create({});

App.controller = Em.Object.create({
    things: [
        Em.Object.create({subthings:[]}),
        Em.Object.create({subthings:[]}),
        Em.Object.create({subthings:[]})
    ],

    allSubThings : function() {
        var things = this.get('things');
        var results = [];
        things.forEach( function(thing)  {
            results.pushObjects( thing.get('subthings') );
        });
        return results;
    }.property('[email protected]').cacheable()
});


setTimeout(function() {
    var things = App.controller.get('things');
    // This works:
    things.objectAt(0).set('subthings',[1,2,3]);
}, 1000);

setTimeout(function() {
    var things = App.controller.get('things');
    // This does not:
    things.objectAt(1).get('subthings').pushObjects([1,2,3]);
}, 2000);

​Thanks!

解决方案

Add another @each to the property list .property('[email protected][email protected]')

http://jsfiddle.net/tomwhatmore/pDQeT/3/

这篇关于使用@each可以观察数组中的数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:25