Accessing parent class in Backbone

后端 未结 8 859
广开言路
广开言路 2020-12-07 14:40

I need to call the initialize method of the parent class, from inside the inherited MyModel-class, instead of completely overwriting it as I am doi

8条回答
  •  伪装坚强ぢ
    2020-12-07 15:07

    here's a multi generation callSuper method, just add it to your extending class.

    callSuper: function (methodName) {
        var previousSuperPrototype, fn, ret;
    
        if (this.currentSuperPrototype) {
            previousSuperPrototype = this.currentSuperPrototype;
            // Up we go
            this.currentSuperPrototype = this.currentSuperPrototype.constructor.__super__;
        } else {
            // First level, just to to the parent
            this.currentSuperPrototype = this.constructor.__super__;
            previousSuperPrototype = null;
        }
    
        fn = this.currentSuperPrototype[methodName];
    
        ret = (arguments.length > 1) ? fn.apply(this, Array.prototype.slice.call(arguments, 1)) : fn.call(this);
    
        this.currentSuperPrototype = previousSuperPrototype;
    
        return ret;
    }
    

提交回复
热议问题