我正在構建一個簡單的應用程序,實現「faceting」或「過濾」接口 - 沒有數據寫入。我希望用戶選擇左側的構面(複選框)以減少右側的結果集。Backbone.js需要幫助連接視圖事件與其他視圖
我有2個獨立的集合(方面和激勵)。這些正從一個包含2個項目的數組的單個json對象填充。每兩個項目有助於爲頁面的這兩個部分創建模型,視圖和集合。
我需要在方面視圖中獲取事件以更新激勵集合的結果。將這兩者連接在一起的最佳方式是什麼?我的測試應用程序可以在這裏找到。我正在使用Handlebars模板。
看到它在這裏:DEMO SITE
主要內容的區域由「汽車激勵」像這樣的。
//define Incentive model
var Incentive = Backbone.Model.extend({
defaults: {
photo: "car.png"
}
});
//define individual incentive view
var IncentiveView = Backbone.View.extend({
tagName: "article",
className: "incentive-container",
template: Handlebars.compile($("#incentive-template").html()),
initialize: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
//define Incentives collection
var Incentives = Backbone.Collection.extend({
model: Incentive,
url: 'data/incentives3.json',
parse: function(response) {
return response.incentiveHolder;
}
});
var IncentivesView = Backbone.View.extend({
el: $(".incentives-container"),
initialize: function() {
this.collection = new Incentives();
// When the contents of the collection are set, render the view.
this.collection.on('reset', function() {
incentives = this.collection.models;
this.render();
}, this);
this.collection.fetch();
this.collection.on("reset", this.render, this);
},
render: function() {
this.$el.find("article")
.remove();
_.each(this.collection.models, function(item) {
this.renderIncentive(item);
}, this);
},
renderIncentive: function(item) {
var incentiveView = new IncentiveView({
model: item
});
this.$el.append(incentiveView.render()
.el);
}
});
該面模型/集合詳述如下。您可以在FacetView中看到 - 當用戶選擇複選框時,我註冊了「更改」事件。我需要一些如何使用這些數據來1)隱藏每個不包含此facet值的激勵或2)從FacetCollection(??)中刪除模型。我希望用戶能夠快速切換這些複選框開啓/關閉一堆 - 刪除/添加較慢(或效率較低),然後隱藏/顯示與CSS?您可以從我的displayIncentives()函數中看到「this」 - 是視圖的一個實例 - 但是如何訪問剛剛單擊的複選框的「值」?
如果我能得到這個值 - 那麼我可以檢查激勵集合,遍歷每個項目 - 並要求它是「激勵」數組「包含」這個剛剛點擊的複選框的值。如果返回false,我會隱藏(或刪除?)該項目並繼續。
// ======================================
// FACETS
// ======================================
//define Facet model
var Facet = Backbone.Model.extend({});
//define individual facet view
var FacetView = Backbone.View.extend({
tagName: "div",
className: "facet-group",
template: Handlebars.compile($("#facets-template").html()),
initialize: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
events: {
"change input[type=checkbox]": "displayIncentives"
},
displayIncentives: function() {
console.log(this);
}
});
//define Facets collection
var Facets = Backbone.Collection.extend({
model: Facet,
url: 'data/incentives3.json',
parse: function(response) {
return response.facetsHolder;
}
});
var FacetsView = Backbone.View.extend({
el: $(".facets-container"),
initialize: function() {
this.collection = new Facets();
// When the contents of the collection are set, render the view.
this.collection.on('reset', function() {
this.render();
}, this);
this.collection.fetch();
},
render: function() {
_.each(this.collection.models, function(item) {
this.renderFacet(item);
}, this);
},
renderFacet: function(item) {
var facetView = new FacetView({
model: item
});
this.$el.append(facetView.render().el);
}
});
最後 - 我的JSON對象就設在這裏......我此刻的自由,以幫助設計這個對象是如何在這個項目的下一階段結構。任何建議不勝感激。
好吧 - 我知道我可以通過訪問事件currentTarget.value來獲取複選框的值。越來越近。 – rsturim
我的答案是否適合並回答您的問題? - –