backbone.stickit を介して双方向データバインドされた 2 つのフォーム要素があります。2 番目のフォーム要素 ( #input ) は単なる装飾です。実際に機能していることを示すためのものです。
アイデアは、ドロップダウン ( #select ) メニュー内のオプションが変更されるたびに、ビューが (再) レンダリングされることです。
#select の「変更された」イベントをキャッチし、 this.render() を呼び出してビューを (再) レンダリングすることで、それを達成しようとしています。
どうやらそれはうまくいきません。選択したオプションがモデルに保存されず、その理由がわかりません。
次のコードが機能しない理由の説明ではなく、解決策を探しているわけではありません。解決策(次のように:私にとってはうまくいく)はフィドルの一部です-コメントアウトされています。
HTML:
<script type="text/template" id="tpl">
<h1>Hello <%= select %></h1>
<select id="select">
</select>
<p>Select:
<%= select %>
</p>
<hr>
<input type="text" id="input">
<p>Input:
<%= input %>
</p>
</script>
<div id="ctr"></div>
JavaScript:
Foo = Backbone.Model.extend({
defaults: {
select: "",
input: "",
}
});
FooView = Backbone.View.extend({
el: '#ctr',
template: _.template($('#tpl').html()),
initialize() {
this.model.bind('change', function() {
console.log("model change:");
console.log(this.model.get('select'));
console.log(this.model.get('input'));
}, this);
//this.model.bind('change:select', function() { this.render(); }, this); // <--------------------- WORKS
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.stickit();
return this;
},
events: {
'change #select': function(ev) {
console.log('change event triggered:');
console.log(this.model.get('select'));
console.log(this.model.get('input'));
this.render(); // <--------------------- DOES NOT WORK - WHY?
},
/* 'click #render': function(ev) {
console.log('render event triggered:');
console.log(this.model.get('select'));
console.log(this.model.get('input'));
this.render();
} */
},
bindings: {
'#input': 'input',
'#select': {
observe: 'select',
selectOptions: {
collection: function() {
return [{
value: '1',
label: 'Foo'
}, {
value: '2',
label: 'Bar'
}, {
value: '3',
label: 'Blub'
}]
}
}
},
},
});
new FooView({
model: new Foo()
}).render();