Created
September 29, 2011 05:37
-
-
Save evangoer/1250052 to your computer and use it in GitHub Desktop.
A more sugary way to create Y.Base derived objects.
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
/* What do we really care about when creating a Base-derived object? | |
1. the object we are deriving from | |
2. string name | |
3. properties and methods | |
4. attributes | |
Goals: simpler method with fewer args & less nesting. Simplify | |
adding attributes. No need for empty [] hack as with Y.Base.create() */ | |
Y.Example = Y.Model.derive('example-model', { | |
// prototype methods and properties here | |
hello: function () { | |
return 'hi'; | |
} | |
}, { | |
// attributes here | |
frobozz: { | |
value: 'zorkmid', | |
readOnly: true | |
}, | |
grue: 17 // MOAR sugar; equivalent to grue: { value: 17 } | |
}, { | |
// publish events here | |
'somethingSpecial': { | |
emitFacade: true, | |
broadcast: 2, | |
} | |
}); | |
/* NOTES: | |
1. Y.Base.derive() extends from Y.Base. Y.Model.derive() extends from | |
Y.Model. And so on. Eliminate an unnecessary arg. | |
2. Assumption: mixins are less important and developers can always do this | |
themselves. Perhaps allow a fourth optional [] argument to make it easier. | |
3. Assumption: adding statics like HTML_PARSER is easy. Developers can do | |
this themselves. Better to focus on making attributes less clumsy to add. */ |
I completely agree. I didn't like the proto as a method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, I think I do like the chained methods approach as sugar for adding attributes and events. And for mixing in extensions. That approach seems cleaner to me than a single method that takes a whole bunch of arguments in a particular order.
Adding prototype methods through a special method seems weird to me, now that I'm looking at it.