【问题标题】:Ember view's attribute shared between view's instances?Ember 视图的属性在视图的实例之间共享?
【发布时间】:2014-06-14 13:08:51
【问题描述】:

我有一个看法:

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}}

在应用程序中,我同时插入了许多 PhotoUploadView;问题是,不是为 PhotoUploadView 的每个实例使用不同的图像数组,而是每个实例都有一个包含所有图像的图像数组,就像该数组在实例之间共享一样;

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

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    Ember 将其视为静态属性,它适用于该视图的所有实例。如果您预先将其设置为未定义,则在初始化时(或在需要时)定义它应该可以解决问题。

    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'))
        });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多