开发者

Sproutcore 2 - Class methods and variables

I have been implementing cla开发者_Python百科ss methods as shown in this answer

Basic Sproutcore: class method, class variables help

but it no longer works in Sproutcore 2:-

MyFooClass.mixin({
    barClassMethod: function() {
        /* ... */
    }
})

gives error

MyFooClass.mixin [undefined] is not a function

I have tried reading through the Sproutcore source and couldn't really identify any Class methods or variables, which quite surprised me.

Maybe i'm just doing it wrong?


Two things you should keep in mind when extending a sproutcore class.

First you have to define your class before you can mix something into it. So start by creating your class/object either with

MyFooClass = SC.Object.extend({ ... });

or

MyFooClass = SC.Object.create({ ... });

whereas the first statement would create a class and the second statement a concrete implementation of a class (to speak in object-oriented terminology a object). Normally, if you are using SC.Object.create() the goal is to create a singleton-object otherwise define your class with SC.Object.extend() and use the defined class to create objects of your defined class with

myFooObject = MyFooClass.create();

So make sure your class or object you want to mix in methods is defined first and is already in scope when you want to use it.

Second, if you want to mixin additional methods or properties into an pre-existing class or object use SC.mixin as follows

SC.mixin(MyFooClass, {
    yourMixedInMethod: function() {
        console.log('mixed in method called');
    }
});

you can do this with objects as well but be aware that only the given object gets the mixed in method and not all instances of the class. If you want all instances to get the additional functionality you have to mix into the class definition.


I think you are looking for

MyFooClass.reopenClass
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜