简体   繁体   English

观察数组并在ember.js中获得更改的值

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

Here's the component: 这是组件:

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

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

    // other code
});

I'd like to get the changed value in _watchColumns method. 我想在_watchColumns方法中获取更改的值。 Is it possible? 可能吗?

I don't think this is possible. 我认为这是不可能的。 A workaround might be to use a component for each item. 解决方法可能是为每个项目使用一个组件。

My-Columns component 我的栏目组件

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

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

My-Column component 我的栏目组件

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

Another tip: never set a property to [] or {} in a component as it will be shared across all instances of the compoent (like a static property - unless you want this behaviour of course). 另一个提示:永远不要在组件中将属性设置为[]{} ,因为它将在组件的所有实例之间共享(例如静态属性-除非您当然希望这种行为)。 In your case the user will override it by passing in their own columns object but it is helpful to know anyway. 在您的情况下,用户将通过传递其自己的column对象来覆盖它,但是无论如何要知道这是有帮助的。 If you need an array in your component you can do this: 如果您的组件中需要数组,则可以执行以下操作:

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