Created
September 26, 2012 20:49
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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