我是BackboneJS的新手。在编写多个GET
实现后,我试图用Backbone JS实现登录屏幕。
骨干登录屏幕
文件夹结构
应用
- >模型
- >查看
- >模板
- >服务器
formSignIn.html
<form class="form-signin" role="form">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="email" id="email" class="form-control" placeholder="Email address" required="" autofocus="">
<input type="password" id="password" class="form-control" placeholder="Password" required="">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
Backb一个查看
var SignInView = Backbone.View.extend({
el:$('.container'),
template:_.template('../templates/formSignIn.html'),
events: {
"click .btn":"signIn"
},
initialize: function() {
this.model.on('change', this.render, this);
},
render: function() {
var attributes = this.model.toJSON();
this.$el.html(this.template(attributes));
},
signIn: function() {
this.model.signIn({
email: $('#email').val(),
password: $('#password').val()
});
}
});
var signInView = new SignInView({model: signInModel});
signInView.render();
骨干示范
var SignInModel = Backbone.Model.extend({
url:function() {
'http://localhost:3000/singIn'
},
defaults: {
email:"",
password:""
},
parse: function(resp) {
return resp;
},
signIn: function() {
this.save();
}
});
var signInModel = new SignInModel();
问题:
HTML模板不渲染。当我打开页面时,显示
../templates/formSignIn.html
。这意味着_template
不能识别该html。视图和模型如何实现?这是正确的做法吗?我对调用模型的
save()
不是很有信心。
我的回答有帮助吗?如果是这样,请考虑将其标记为正确的答案。 – bejonbee