Created
March 19, 2010 16:11
-
-
Save tomdale/337732 to your computer and use it in GitHub Desktop.
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
// dynamically apply a mixin specified in an object property | |
var MyClass = SC.Object.extend({ | |
extraMixin: null, | |
foo: "bar", | |
init: function() { | |
this.mixin(this.extraMixin); | |
arguments.callee.base.apply(this, arguments); | |
}, | |
someProperty1: function(){ | |
console.log("in someProperty1") | |
return this.get('foo') + " : something1" | |
}.property('foo').cacheable() | |
}); | |
var ExampleMixin = { | |
someProperty2: function(){ | |
console.log("in someProperty2") | |
return this.get('foo') + " : something2" | |
}.property('foo').cacheable() | |
}; | |
var instance = MyClass.create({ | |
extraMixin: ExampleMixin | |
}) ; | |
/* | |
console output | |
>>> instance.get('foo') | |
"bar" | |
>>> instance.get('someProperty1') | |
in someProperty1 | |
"bar : something1" | |
>>> instance.get('someProperty2') | |
in someProperty2 | |
"bar : something2" | |
>>> instance.get('someProperty1') | |
"bar : something1" | |
>>> instance.get('someProperty2') | |
"bar : something2" | |
>>> instance.set('foo', "something else") | |
MyClass:sc554 { extraMixin=Object, more...} | |
>>> instance.get('foo') | |
"something else" | |
>>> instance.get('someProperty1') | |
in someProperty1 | |
"something else : something1" | |
>>> instance.get('someProperty2') | |
"bar : something2" | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment