ArrayProxy-Emberjs

时间:2023-03-09 18:10:39
ArrayProxy-Emberjs

ember 2.18版本API翻译之Ember.ArrayProxy

import ArrayProxy from '@ember/array/proxy';
ArrayProxy(数组代理)包装实现Ember.Array和/或Ember.MutableArray的任何其他对象,转发所有请求。 这对于大量绑定用例或其他能够交换基础数组的情况非常有用非常有用。对于大量的绑定或其他会交换出底层数组的情况非常有用。
    let pets = ['dog', 'cat', 'fish'];
let ap = Ember.ArrayProxy.create({ content: Ember.A(pets) }); ap.get('firstObject'); // 'dog'
ap.set('content', ['amoeba', 'paramecium']);
ap.get('firstObject'); // 'amoeba'

这个类同样适用于当被访问时,改变数组的内容。可以通过重写objectAtContent来做:

    let pets = ['dog', 'cat', 'fish'];
let ap = Ember.ArrayProxy.create({
content: Ember.A(pets),
objectAtContent: function(idx) {
return this.get('content').objectAt(idx).toUpperCase();
}
});
ap.get('firstObject'); // . 'DOG'

原文链接