Skip to content

Instantly share code, notes, and snippets.

@syabro
Created March 17, 2012 00:11
Show Gist options
  • Save syabro/2053813 to your computer and use it in GitHub Desktop.
Save syabro/2053813 to your computer and use it in GitHub Desktop.
$(function (App) {
App.Views.CategoryListView = Backbone.View.extend({
tagName: 'ul',
collection: new App.Collections.CategoryCollection(),
events: {
'click .staff_pick': "showStaffPick"
},
initialize: function (arguments) {
this.page = arguments.page;
this.collection.bind('add', this.onCollectionAdd, this);
this.collection.bind('reset', this.renderCategories, this);
this.collection.bind('all', this.render, this);
},
onCollectionAdd: function (category) {
var view = new App.Views.CategoryListItemView({ model: category, list: this });
$(this.el).append(view.render().el);
},
clearSelection: function () {
this.$('li').removeClass('selected');
},
renderCategories: function () {
this.collection.each(this.onCollectionAdd, this);
},
render: function () {
var self = this;
$(this.el).html($('<li class="staff_pick selected"><a href="javascript:;">Рекомендуемые</a></li>').click(function () {
self.clearSelection();
$(this).addClass('selected');
}));
this.renderCategories();
return this;
},
loadTopCategories: function () {
this.collection.loadTopCategories();
},
showStaffPick: function () {
window.page.loadSoftware({ staff_pick: true });
}
});
App.Views.CategoryListItemView = Backbone.View.extend({
template: '#category_template',
tagName: 'li',
events: {
'click a': "loadCategory"
},
initialize: function (arguments) {
this.page = arguments.page;
this.model = arguments.model;
this.list = arguments.list;
},
loadCategory: function () {
this.list.clearSelection();
$(this.el).addClass('selected');
this.list.page.loadSoftware({ category: this.model.get('id') });
},
render: function () {
var template = _.template($(this.template).html());
$(this.el).html(template(this.model.toJSON()));
return this;
}
});
}(window.App));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment