Backbone JS事件没有解雇

时间:2013-09-18 04:37:27

标签: javascript jquery backbone.js backbone-events

我无法让骨干事件发挥作用。我只想模拟表单提交并获得警报,但我似乎无法到达那里......

我的标记如下:

<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>

我的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 });
});

1 个答案:

答案 0 :(得分:2)

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