Best practices with Ember ArrayProxy

Ember's Em.ArrayProxy and Em.Array have many programatic methods available for notifying observers of changes to content . For example:

  • arrayContentDidChange
  • arrayContentWillChange
  • enumerableContentDidChange
  • enumerableContentWillChange
  • contentArrayWillChange
  • Em.ArrayProxy also has several methods for manipulating the ArrayProxy's content. For example:

    this.pushObject('something random');
    // Or
    this.insertAt(2, 'something random');
    

    When using the latter methods, does one have to use them in conjunction with the former methods? It seems silly that Ember's usually-automated property observers would require a manual kick here but I don't find the documentation very clear.


    No, you don't have to use any methods in conjunction.

    If you want to add items to your ArrayProxy, simply pushObject() . You would know this by just using the method and seeing that it just works.

    From the docs:

    This mixin implements Observer-friendly Array-like behavior. It is not a concrete implementation, but it can be used up by other classes that want to appear like arrays.

    http://emberjs.com/api/classes/Ember.Array.html

    Ember.Array is a type of class that in other programming languages (without mixins) receives the name of an interface.

    An ArrayProxy wraps any other object that implements Ember.Array

    http://emberjs.com/api/classes/Ember.ArrayProxy.html

    Ember.ArrayProxy is exactly what the name says, a proxy, that wraps around any object that has implemented the Ember.Array interface.

    The other methods, that you mention, might be implemented/overridden if you are making your own "subclass" of Ember.Array . Some must be implement to make your subclass ArrayProxy friendly. Or if you want to add custom behaviour, maybe write to a log whenever arrayContentDidChange , then you override that method and add whatever logic your app needs.

    That is Object Oriented Programming and all those explanations are out of scope for the documentation of any framework.


    Are you asking whether pushObject et cetera trigger those events?

    From the documentation for insertAt :

    This will use the primitive replace() method to insert an object at the specified index.

    From the documentation for replace :

    You should also call this.enumerableContentDidChange()

    So, yes, a properly implemented ArrayProxy will trigger those events when you add or remove things.

    链接地址: http://www.djcxy.com/p/65816.html

    上一篇: Ember方式来实现搜索对话框

    下一篇: Ember ArrayProxy的最佳做法