Where is the oldValue in published properties?

I read this article:
http://enyojs.com/docs/latest/developer-guide/key-concepts/objects-and-published-properties.html

And I write my code like this:
enyo.kind({ name: "Ok.Panorama.Block", kind: "Control", published: { bounds: {} }, create: function () { this.inherited(arguments); this.set('bounds', arguments.bounds); }, boundsChanged: function (oldBounds) { console.log(oldBounds); console.log(this.bounds); } });

Due to the developer guide, I thought the input parameter of boundsChanged is the old value. But actually, the oldBounds is the argument.bounds, and the this.bounds is undefined.

Please explain this. How do I use the old value?

Thanks very much.

Comments

  • The signature for an observer function is:

    propertyChanged: function (was, is, prop)

    However, when a property is set on the component configuration, you won't see a change notification because the properties are set before the observers are initialized.

    In your example, arguments.bounds will be undefined because the native arguments object doesn't have a bounds property. If your expectation was that arguments.bounds was going to contain the value of bounds specified in the component configuration, you can either use the first argument to create() or the value on this since it will have already been mixed in.
    create: function (props) {
        this.inherited(arguments);
        // generally true unless specially manipulated ...
        this.bounds === props.bounds;
    }
    The common convention is to manually call any change handlers in create() that you need ran.
    create: function (props) {
        this.inherited(arguments);
        this.boundsChanged();
    }
  • It's also worth noting that using objects as initializers for properties is generally a bad idea. Also, observers cannot detect changes to members of objects. If you were to, for example, instantiate two Ok.Panorama.Block and then do 'this.bounds.key = 5' both instances would be updated with the bounds, probably not what you intended. If you want to use an object, best to initialize it in create.
Sign In or Register to comment.