Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone: Call an extended view's overridden render() function

I have a WorkoutExerciseRowView which extends ExerciseRowView. The render functions are extremely similar, except the WorkoutExerciseRowView must add a few parameters to ExerciseRowView's render. How can I call ExerciseRowView's render function inside WorkoutExerciseRowView's render function?

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
        });
    }
});

Thanks!

like image 672
Garrett Avatar asked Jun 27 '12 03:06

Garrett


People also ask

What is backbone view extend?

The Backbone. js view extend method is used to extend the Backbone. js view class to create a custom view. Syntax: Backbone.

Is Backbone JS still used?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is backbone JS used for?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.


1 Answers

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
        });
    }
});

From Backbone's documentation here: http://backbonejs.org/#Model-extend

Brief aside on super: JavaScript does not provide a simple way to call super — the function of the same name defined higher on the prototype chain. If you override a core function like set, or save, and you want to invoke the parent object's implementation, you'll have to explicitly call it, along these lines:

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

like image 76
Bobby Avatar answered Sep 28 '22 04:09

Bobby