Why Backbone.js isn't binding my event

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2012-03-20T22:57:02Z Indexed on 2012/03/20 23:30 UTC
Read the original article Hit count: 212

Filed under:
|
|

I have a router like this, as main entry point:

window.AppRouter = Backbone.Router.extend({
    routes: {
        '': 'login'
    },
    login: function(){

        userLoginView = new UserLoginView();
    }
});

var appRouter = new AppRouter;
Backbone.history.start({pushState: true});

I have a model/collection/view like this:

window.User = Backbone.Model.extend({});

window.Users = Backbone.Collection.extend({
    model: User
});

window.UserLoginView = Backbone.View.extend({

    events: {
        'click #login-button': 'loginAction'
    },

    initialize: function(){
        _.bindAll(this, 'render', 'loginAction');
    },

    loginAction: function(){
        var uid = $("#login-username").val();
        var pwd = $("#login-password").val();

        var user = new User({uid:uid, pwd:pwd});
    }
});

And body of my HTML looks like this:

<form action="#" method="POST" id="login-form">
    <p>
        <label for="login-username">username</label>
        <input type="text" id="login-username" autofocus />
    </p>
    <p>
        <label for="login-password">password</label>
        <input type="password" id="login-password" />
    </p>
    <a id="login-button" href="#">Inloggen</a>
</form>

Note: The HTML comes from Node.js using express.js, should I maybe wait for a document ready event somewhere?

Edit:

I have tried this, create the view when ready, did not solve the problem.

    $(function(){
        userLoginView = new UserLoginView();
    });

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events