0
我試圖使用backbone.stickit庫綁定我的表單輸入到模型,但似乎無法讓模型正確更新。模型不更新表單提交,使用Backbone + Stickit
KeyUp事件似乎正常工作,我可以看到的價值變化,如果我使用「開始」回調來顯示它:
bindings: {
'#firstName': {
observe: 'firstName',
onSet: function(val, options) {
$('#output').html(val);
}
}
}
這裏是我的代碼(Run it on jsfiddle):
HTML
<div id="view">
<form name="form" id="form">
<input id="firstName" type="text"/>
<input type="submit" id="submit"/>
</form>
<div id="output"></div>
</div>
的JavaScript
var app = {
Model: Backbone.Model.extend({
firstName: 'test'
}),
View: Backbone.View.extend({
el: "#view",
initialize: function(){
this.model = new app.Model();
this.render();
},
bindings: {
'#firstName': 'firstName'
},
render: function(){
this.$el.html(this.template);
this.stickit();
},
events: {
"submit #form": "submitForm"
},
submitForm: function(e) {
e.preventDefault();
$('#output').html('output:'+this.model.firstName);
}
})
};
var view = new app.View();
非常感謝你,這樣做。我不能相信我錯過了這一點,使得完美的感覺:-) –