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'
本文深入探讨了Ember.js中ArrayProxy的使用方法及其在数组操作中的灵活性。ArrayProxy能够包装任何实现了Ember.Array的对象,提供了强大的绑定能力和数组内容动态更新机制。通过实例展示了如何利用ArrayProxy进行数组元素的获取和设置,以及如何通过重写objectAtContent方法来改变数组内容。
273

被折叠的 条评论
为什么被折叠?



