0

I'm having trouble getting backbone events working. I simply want to simulate a form submission and get an alert, but I can't seem to get there...

My markup is as follows:

<form method="post" id="userSignIn">       
   <label>Email</label> 
   <input type = "text" name = "useremail" value = "" id="userEmail"> 

   <br />

   <label>Pass</label>
   <input type = "password" name = "userpass" value = "" id="userPassword">

   <br /><input type = "submit" value = "Submit">
</form>

My JS

var events = _.clone(Backbone.Events);

var SigninModel = Backbone.Model.extend({
    url: 'http://localhost/api/app/User.php',
});

var SignInCollection = Backbone.Collection.extend({
    model: SigninModel
})

var SigninView = Backbone.View.extend({
    events: {
        'submit #userSignIn': 'signIn'
    },

    initialize: function() {
        console.log('Sign in view initialized');
    },

    signIn: function(e) {
        alert("Your username is" + $('#userSignIn #userEmail'));
        e.preventDefault();
    }
});

$(document).ready(function(){
    var signInCollection = new SignInCollection();
    new SigninView({ el: $('#userSignIn'), collection: signInCollection });
});
4

1 回答 1

2

Backbone.View.events 只会查找由 DOM 元素触发的事件,这些元素是视图元素(或该元素的子元素)的一部分。理想情况下,您将通过视图上的 render 方法呈现此 HTML,但另一种方法是从 SignInView.initialize 内部调用 setElement

于 2013-09-18T04:46:00.790 回答