簡體   English   中英

觀察數組並在ember.js中獲得更改的值

[英]Observe array and get changed value in ember.js

這是組件:

export default Ember.Component.extend({
    columns: []

    _watchColumns: function () {
        // console.log(changedValue);
    }.observes('columns.@each.name')

    // other code
});

我想在_watchColumns方法中獲取更改的值。 可能嗎?

我認為這是不可能的。 解決方法可能是為每個項目使用一個組件。

我的欄目組件

//my-columns.js
export default Ember.Component.extend({
    columns: null
});

//my-columns.hbs
{{#each columns as |column|}}
    {{my-column value=column}}
{{/each}}

我的欄目組件

//my-column.js
export default Ember.Component.extend({
    nameUpdated : Ember.computed('value.name', function() {
        console.log(this.get('value.name'));
    });
});

另一個提示:永遠不要在組件中將屬性設置為[]{} ,因為它將在組件的所有實例之間共享(例如靜態屬性-除非您當然希望這種行為)。 在您的情況下,用戶將通過傳遞其自己的column對象來覆蓋它,但是無論如何要知道這是有幫助的。 如果您的組件中需要數組,則可以執行以下操作:

export default Ember.Component.extend({
    myArray : null,
    init : function() {
        this._super.apply(this, arguments);
        this.set('myArray', []);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM