ezhil
ezhil

Reputation: 1083

Issue with calling function from View in Backbone

I have created model and view in backbone. It gives me a syntax error as "SyntaxError: function statement requires a name - >changeClg: function (height, width) {"

selectTable = Backbone.View.extend({
    render: function() {
        var height= 50;
        var width= 56;
        if (true) {
            changeClg(height,width);
        }
        changeClg: function (height, width) {
            console.log("inside changeClg");
        }
        var selector = this.$el.find("select.clg-selection");
        selector.html("<option></option>");
    }
});

But it is working when i declare that changeClg function outside of the View with different syntax as below

selectTable = Backbone.View.extend({
    render: function() {
        var height= 50;
        var width= 56;
        if (true) {
          changeClg(height,width);
        }
        var selector = this.$el.find("select.clg-selection");
        selector.html("<option></option>");
    }
});
changeClg = function (height, width) {
    console.log("inside changeClg");
    this.$el.find(".clg-dors")[0];
}

Actually i need to use el object and also some other objects so i must go to the first option but it is giving me the error.

i just calling "changeClg" function but it is giving me the syntax error.

I would like to know that what is the different between these two function declaration?

Can anyone please tell me what could be the problem?

Upvotes: 0

Views: 116

Answers (2)

2292amit
2292amit

Reputation: 145

I think changeClg: function (height, width) is not the correct syntax. You should be using `var changeClg = function (height, width){...} instead.

The problem you are facing is since you are not creating an object using a: function is wrong i.e. c = {a: function{...}} is right and you can use c.a() but a: function{...} is wrong, instead var a = function {...} is the correct way to do it and you can use a() directly

Upvotes: 1

ezhil
ezhil

Reputation: 1083

It is working we should use "this" keyword to call object property and also i created "changeClg" function after render property as below

selectTable = Backbone.View.extend({
    render: function() {
    var height= 50;
    var width= 56;
        if (true) {
          this.changeClg(chartHeight);
        }
      var selector = this.$el.find("select.clg-selection");
      selector.html("<option></option>");
    },
    changeClg: function (height, width) {
        console.log("inside changeClg");
    }
});

Thanks

Upvotes: 0

Related Questions