我是Backbone.js的初學者,我很難在兩個Backbone模型之間建立一對多關係芯片,並將這些數據顯示在html tabel行中。 我正在使用可以有多個訂單的機器。數據將如下所示:骨幹一對多關係芯片
- 機器10的訂單爲1,2,5,9。
- 機器14的訂單3,4,6。
機器和訂單與FK machine_id按順序結合在一起。 我試圖讓第一個元素TH的TR元素成爲機器,第二個/第三個TH我想要顯示屬於機器的訂單。
所以我想問的問題是:
- 如何使這種一對多的關係,我骨幹模型中。
- 我如何在我的下劃線模板中使此TR元素顯示屬於機器的訂單。
以下是訂購的型號與機器:
app.Machine = Backbone.Model.extend({
defaults : {
machine_id : "",
status : "",
description : "",
},
});
app.Order = Backbone.Model.extend({
defaults: {
order_id: "",
description: "",
amount: "",,
begin_date:"",
end_date:"",
machine_id: ""
},
});
app.MachineList = Backbone.Collection.extend({
model: app.Machine
});
瀏覽:
app.MachineView = Backbone.View.extend({
className: 'MachineRow',
template: _.template($('#Machine_Template').html()),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});
app.MachineListView = Backbone.View.extend({
el: '#Machine',
initialize: function(initialMachine){
this.collection = new app.MachineList(initialMachine);
this.render();
},
render: function(){
this.collection.each(function(item){
this.renderMachine(item);
filterOrder(item.get('machine_id'));
}, this);
},
renderMachine: function(item){
var machineView = new app.MachineView({
model: item
});
$(machineView.render().el).appendTo(this.$el)
}
});
HTML代碼:會是這個樣子?
<script id="machine_template" type="text/template">
<th>
<%= machine_id %> //etc
</th>
<th>
<%= order_id %> //etc
</th>
<th>
<%= order_id %> //etc
</th>
</script>