Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.69 KB

Published-Properties.md

File metadata and controls

51 lines (38 loc) · 1.69 KB

Published Properties

enyo.Object implements the Enyo framework's property publishing system. Published properties are declared in a hash called published within a call to enyo.kind. Getter and setter methods are automatically generated for properties declared in this manner. Also, by convention, the setter for a published property will trigger an optional <propertyName>Changed method when called.

In the following example, myValue becomes a regular property on the MyObject prototype (with a default value of 3), and the getter and setter methods are generated as noted in the comments:

enyo.kind({
	name: "MyObject",
	kind: enyo.Object,

	// declare 'published' properties
	published: {
		myValue: 3
	},

	// These methods will be automatically generated:
	//	getMyValue: function() ...
	//	setMyValue: function(inValue) ...

	// optional method that is called whenever setMyValue is called
	myValueChanged: function(inOldValue) {
		this.delta = this.myValue - inOldValue;
	}
});

Since we have declared a changed method (i.e., myValueChanged) to observe set calls on the myValue property, it will be called whenever setMyValue is called, as illustrated by the following:

myobj = new MyObject();
var x = myobj.getMyValue(); // x gets 3

myobj.setMyValue(7); // myValue becomes 7; myValueChanged side-effect sets delta to 4

Changed methods are called whenever setters are invoked, whether or not the actual value has changed.

Published properties are stored as regular properties on the object prototype, so it's possible to query or set their values directly:

var x = myobj.myValue;

Note that when you set a property's value directly, the changed method is not called.