mike628
mike628

Reputation: 49351

backbone extend and calling functions

I think I'm misunderstanding some scoping issues. I am running the TODO backbone app and after the "new app.AppView();"

I am trying this: app.AppView.render()

as well as other functions that I thought were being extended, but they seem unavailable.

The second question is ..Why does chrome developer tools say "child" next to TODORouter and Todos

EDIT The todo link TODO

This is the code I'm mostly reffering to

$(function() {

    // Kick things off by creating the **App**.
    new app.AppView();

});

Upvotes: 1

Views: 183

Answers (2)

jakee
jakee

Reputation: 18556

You don't have scoping issues, but kind of 'coping with javascript' -issues.

at app.AppView is stored a Function -object named AppView. In javascript functions are used as 'classes' (don't think about Java classes!) in a system called prototype inheritance. Don't get mixed into that yet.

When you call

new app.AppView()

you create a new instance of this AppView 'class', that IS an object. So when you call

app.AppView.render()

you're trying to call the function render of the 'class' (or class-but-not-quite-a-class). Now that isn't right one bit.

So (like in Java or any other oo-language), you have to store the instance you get by calling the constructor to a variable.

var appView = new app.AppView();

Now that you have an instance, you can do whatever you like with it

appView.render();

Upvotes: 1

ericponto
ericponto

Reputation: 736

Save off your new appview in a var and then use it from there.

var myAppView = new app.AppView();
// ...
myAppView.render();

Upvotes: 2

Related Questions