Skip to content

Instantly share code, notes, and snippets.

@rgrove
Created September 26, 2012 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrove/3790499 to your computer and use it in GitHub Desktop.
Save rgrove/3790499 to your computer and use it in GitHub Desktop.
WIP on an Object.defineProperty() facade for YUI (as an alternative to Y.Attribute)
function Property() {}
Property.prototype = {
// -- Public Prototype Methods ---------------------------------------------
defineProperties: function (properties) {
return Property.defineProperties(this, properties);
},
defineProperty: function (name, descriptor) {
return Property.defineProperty(this, name, descriptor);
},
get: function (name) {
return this[name];
},
getProperties: function (names) {
var properties = {},
i, len, name;
for (i = 0, len = names.length; i < len; i++) {
name = names[i];
result[name] = this.get(name);
}
return properties;
},
set: function (name, value) {
return this[name] = value;
},
setProperties: function (properties, options) {
for (var name in properties) {
if (properties.hasOwnProperty(name)) {
this.set(name, properties[name], options);
}
}
}
};
// -- Static Methods -----------------------------------------------------------
Property.defineProperties = function () {
return Object.defineProperties.apply(null, arguments);
};
Property.defineProperty = function () {
return Object.defineProperty.apply(null, arguments);
};
Y.Property = Property;
var oldSet = Y.Property.prototype.set;
Y.augment(Y.Property, Y.EventTarget);
Y.Property.prototype.set = function (name, value, options) {
var prevVal = this.get(name),
newVal = oldSet.call(this, name, value, options);
this.fire(name + 'Change', Y.merge(options, {
newVal : newVal,
prevVal : prevVal,
propertyName: name
}));
return newVal;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment