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