6

我有一个WorkoutExerciseRowViewwhich extends 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
        });
    }
});

谢谢!

4

3 回答 3

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 或 save 这样的核心函数,并且你想调用父对象的实现,你必须显式地调用它,如下所示:

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

于 2012-06-27T04:00:37.477 回答
7

你应该可以使用

ExerciseRowView.prototype.render.call(this)

这将从ExerciseRowView 调用渲染函数,范围设置为此(当前模型)

于 2012-06-27T04:02:15.097 回答
1
this.constructor.__super__.render.call(this,{});
于 2014-07-31T19:05:30.727 回答