Skip to content

Instantly share code, notes, and snippets.

@clarle
Created September 20, 2013 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarle/6644290 to your computer and use it in GitHub Desktop.
Save clarle/6644290 to your computer and use it in GitHub Desktop.
Simple computed Attribute
YUI().use("attribute", "attribute-computable", function (Y) {
function Person(options) {
var defaultAttrs = {
firstName : {
value: "John"
},
lastName : {
value: "Doe"
},
fullName : {
// Possibly default to true if `attribute-computable` is used?
computed: true,
// `getter` is evaluated upon instantiation. If getter uses `this.get(attr)`,
// then subscriptions are set up from this attribute to those attributes.
getter: function () {
return this.get('firstName') + ' ' + this.get('lastName');
},
setter: function (value) {
var lastSpacePos = value.lastIndexOf(" ");
if (lastSpacePos > 0) { // Ignore values with no space character
this.set('firstName', value.substring(0, lastSpacePos));
this.set('lastName', value.substring(lastSpacePos + 1));
}
}
}
};
this.addAttrs(defaultAttrs, options);
}
Y.augment(Person, Y.Attribute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment