Update a static property from outside the kind

Hi,

I have an issue with statics properties on a kind. I declare a static variable in my kind, then I need to set it from outside this kind. The thing is that this doesn't work.
Here's an example: http://jsfiddle.net/Bm8RX/1/

The alert should display "a, b, c" but displays an empty string. Why is the array empty?

Comments

  • You aren't setting the statics because MyKind hasn't been instantiated yet. It works if you move the array setting line to the line following the "new MyKind()..." declaration.
    http://jsfiddle.net/Bm8RX/2/
  • edited July 2014
    The reason @SebT‌ 's code doesn't work is that enyo employs deferred constructors to prevent building kinds before they're used. So, when you set foo before you've created an instance of MyKind, you're setting it on a different object than you're alerting on in your method. That's why @bbito 's code works; an instance of MyKind was created thus fully initializing the constructor. Check out Oop.js for the internals if you're curious.

    You can turn this behavior off by adding noDefer: true to your kind's definition.

    http://jsfiddle.net/Bm8RX/4/
  • edited July 2014
    OK good, I didn't know constructors were deferred in enyo.
    Thanks for the help!
  • edited July 2014
    @theryanjduffy, thanks for the much better explanation, but it inspired me to look at the 2.4 docs for statics and my read of that page seems to indicate that the use of (public)statics should enforce a noDefer, and that only protectedStatics should show the behavior we are seeing.
    enyojs.com/docs/2.4.0/key-concepts/kinds.html
    Unlike public statics, protected statics won't prevent the deferral of a kind's creation. Deferral is not possible for kinds containing public statics because of the potential for the public statics' being used before any instances of the kind exist.
    So it seems that public statics that are set within the kind's constructor can be read prior to instantiating an instance*, but cannot be manipulated from outside the constructor until an instance is created - is that right?
    [EDIT] * public statics can be read AND set prior to instantiating:
    jsfiddle.net/Bm8RX/6/
    -Pardon my confusion!
    [EDIT2] * I think I was right the first time...:
    jsfiddle.net/Bm8RX/7/

  • edited July 2014
    That appears to be right. The statics hash gets copied onto the deferred constructor but protectedStatics does not. Both are copied again onto the real constructor once it is initialized so if you modify a static member, those changes are lost.
  • @theryanjduffy‌, thanks for the info, I think the docs I referred to are a little unclear since the behavior of statics is different when the kind is set to noDefer. That part of the docs led me to believe that including statics in a kind definition prevented deferral but it seems that is only partially true...
Sign In or Register to comment.