私はアイテムのコレクションを持っていますが、属性の共有ステムの外では(ID、タイトルのような)いくつかのデータを共有しています。ビジネスロジック。Backbone.jsの混合コレクションに最適なデザインパターン
私の問題は、バックボーンスタイルのMVCでの経験がなくて、私はそれぞれの賛否両論は分かりません。もしかするともっと上品な解決策があるのかもしれません。私が潜在的に使用できる3つのテクニックの例は次のとおりです。
var gizmoCollection = new Backbone.Collection(); // or extend
var gizmoModel = Backbone.Model.extend({ ... });
var morgView = Backbone.View.extend({ ... });
var blarView = Backbone.View.extend({ ... });
// 1.) Create an attribute for the view in the model?
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1': view: morgView }));
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2': view: blarView }));
// 2.) Or create a seperate model for each type of model?
var morgModel = morgModel.extend({});
var blarModel = blarModel.extend({});
gizmoCollection.add(new morgModel({ title: 'Gizmo1' });
gizmoCollection.add(new blarModel({ title: 'Gizmo2' });
// 3. Or register 'types' of views?
gizmoView.subClassView('morg', morgView);
gizmoView.subClassView('blar', blarView);
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1', type: 'morg' });
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2', type: 'blar' });