Backbone:调用扩展视图的重写render()函数

时间:2012-06-27 03:47:18

标签: javascript view backbone.js override

我的WorkoutExerciseRowView扩展了ExerciseRowView。渲染函数非常相似,除了WorkoutExerciseRowView必须向ExerciseRowView的渲染添加一些参数。如何在ExerciseRowView的渲染函数中调用WorkoutExerciseRowView的渲染函数?

var WorkoutExerciseRowView = ExerciseRowView.extend( {      
    render : function() {
        //return this.constructor.render({ // doesn't work
        return this.render({ // doesn't work
            workoutExercise : this.model,
            exercise : this.model.get("exercise"),
            workoutSection : this.model.get("section"),
            isEditable : true,
            number : this.number,
            WorkoutExercise : WorkoutExercise,
            WorkoutSection : WorkoutSection
        });
    }
});

谢谢!

3 个答案:

答案 0 :(得分:14)

var WorkoutExerciseRowView = ExerciseRowView.extend( {      
    render : function() {
        return ExerciseRowView.prototype.render.call(this,{
            workoutExercise : this.model,
            exercise : this.model.get("exercise"),
            workoutSection : this.model.get("section"),
            isEditable : true,
            number : this.number,
            WorkoutExercise : WorkoutExercise,
            WorkoutSection : WorkoutSection
        });
    }
});

来自Backbone的文档:http://backbonejs.org/#Model-extend

  

简要介绍一下super:JavaScript没有提供一种简单的调用方式   super - 在原型上定义更高的同名函数   链。如果你覆盖核心功能,比如set,或者你想要保存   要调用父对象的实现,你必须这样做   明确地称它为:

Backbone.Model.prototype.set.call(this, attributes, options);

答案 1 :(得分:7)

你应该可以使用

ExerciseRowView.prototype.render.call(this)

这将从ExerciseRowView调用render函数,并将范围设置为this(当前模型)

答案 2 :(得分:1)

this.constructor.__super__.render.call(this,{});