Skip to content

Instantly share code, notes, and snippets.

@tomdale

tomdale/gist:337732

Created Mar 19, 2010
Embed
What would you like to do?
// 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