Created
September 20, 2013 21:38
-
-
Save clarle/6644290 to your computer and use it in GitHub Desktop.
Simple computed Attribute
This file contains 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
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