在视图的实例之间共享Ember视图的属性?

时间:2014-04-28 20:15:19

标签: ember.js

我有一个观点:

App.PhotoUploadView = Ember.View.extend({
    images: [],
    didInsertElement: function() {
    var that = this;
    var product = this.get('controller').get('model');
    var upimages = product.get('upimages');

    //this.set('images', []);

    upimages.then(function(images) {
        images.forEach(function(image, indexI) {
            var imageObject = new Object();

            imageObject.link = App.appConf.apiPaths.images + image.get('link');
            that.get('images').pushObject(imageObject);
        });
        console.log(that.get('images'))
    });
    }
});

所以我在这里做的是定义一个最初为空的图像数组,并用操纵一些模型的孩子获得的对象填充它......

{{#each product in model}}
    {{view App.PhotoUploadView}}
{{/each}}

在App中我同时插入了很多PhotoUploadView;问题是,不是为PhotoUploadView的每个实例都有一个不同的图像数组,我得到的是每个实例都有一个包含所有图像的图像数组,就好像数组在实例之间共享一样;

如果我删除对this.set的评论('images',[]);在didInsertElement函数中,一切正常;所以问题是:图像数组是在PhotoUploadView实例之间共享的吗?或者我错过了什么......?

1 个答案:

答案 0 :(得分:1)

Ember认为它是一个静态属性,它适用于该视图的所有实例。如果你事先将它设置为未定义,那么在init上定义它(或者在需要的时候)它应该解决问题。

App.PhotoUploadView = Ember.View.extend({
    init: function(){
       this._super();
       this.set('images', []);
    }
    images: undefined,
    didInsertElement: function() {
    var that = this;
    var product = this.get('controller').get('model');
    var upimages = product.get('upimages');

    //this.set('images', []);

    upimages.then(function(images) {
        images.forEach(function(image, indexI) {
            var imageObject = new Object();

            imageObject.link = App.appConf.apiPaths.images + image.get('link');
            that.get('images').pushObject(imageObject);
        });
        console.log(that.get('images'))
    });
    }
});