0

Possible Duplicate:
Javascript , event handler is always called, even if the event is not raised

I'm new to Backbone.js and I have an issue with an event.

In my appView.js, I have just this :

var languagesView = new app.LanguagesView();

And this languagesView.js :

var app = app || {};

$(function() {

app.LanguagesView = Backbone.View.extend({

    initialize: function(){
        this.on("foo", this.fooFunction());
    },

    fooFunction: function() {
        console.log("---FOO----")
    }

});
});

I have no trigger in my app and I don't understand why the fooFunction is always called when I open my page. Is there something I have misunderstood about the events?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Johann
  • 447
  • 2
  • 8
  • 23
  • 3
    Because you *are* calling the function... by the function-invoking parentheses `()`. – Rob W Sep 21 '12 at 13:58
  • Ohoh, thanks ! God forgive me! – Johann Sep 21 '12 at 14:04
  • @RobW I think this question is different enough since the reference is solely about jQuery while this one is presented on top of a Backbone layer, specifically how the Backbone.Events module works. Also, your comment looks like an answer... Collect your prize! :-D – jmk2142 Sep 21 '12 at 21:19
  • @orangewarp The problem is library-independent. The problem is identical, and the duplicate already explains the solution very well. A closre-as-duplicate fits in this case. – Rob W Sep 21 '12 at 22:21

0 Answers0