
Untitled
By: a guest on
Aug 8th, 2012 | syntax:
JavaScript | size: 1.63 KB | hits: 9 | expires: Never
var SelectedPartView = Backbone.View.extend({
tagName: "div",
className: "selectedPart",
template: _.template($("#selectedPart-template").html()),
initialize: function() {
this.model.on("change", this.render, this);
this.model.on("removeSelectedPartView", this.destroy, this);
},
render: function() {
if (debug) console.log("rendering selectedpartview for",this.model.get("name"));
this.$el.html(this.template({part: this.model.toJSON()})).fadeIn(fadeDuration);
return this;
},
destroy: function() {
if (debug) console.log("destroying selectedpartview for",this.model.get("name"));
var self = this;
self.model.off("change", this.render, this);
self.model.off("removeSelectedPartView", this.destroy, this);
self.remove();
}
});
var SelectedPartListView = Backbone.View.extend({
el: $(".selectedPartList"),
initialize: function() {
this.$el.empty();
this.collection.on("add", this.addSubView, this);
this.collection.on("remove", this.removeSubView, this);
this.collection.on("reset", this.reset, this);
},
render: function() {
if (debug) console.log("rendering selectedpartlistview for",this.collection.models);
var self = this;
self.collection.each(function(model) {
self.addSubView(model);
});
return self;
},
reset: function() {
this.$el.empty();
this.render();
},
addSubView: function(model) {
if (model.get("name").toUpperCase() !== "NONE") {
var sv = new SelectedPartView({model: model});
this.$el.append(sv.render().el);
}
},
removeSubView: function(model) {
model.trigger("removeSelectedPartView");
}
});