Skip to content

Instantly share code, notes, and snippets.

@fxposter
Created April 21, 2011 16:05
Show Gist options
  • Save fxposter/934866 to your computer and use it in GitHub Desktop.
Save fxposter/934866 to your computer and use it in GitHub Desktop.
var Search = Backbone.Model.extend({
setSelectedTagIds: function(ids) {
clearTimeout(this.timeout);
var model = this;
model.set({ selected_tag_ids: ids, update_type: 'tags' });
model.timeout = setTimeout(function() {
model.fetch();
}, 1000);
},
setRecommendationsType: function(type) {
clearTimeout(this.timeout);
this.set({ recommendations_type: type, update_type: 'recommendations' });
this.fetch();
},
setLocationId: function(id) {
clearTimeout(this.timeout);
this.set({ location_id: id, update_type: 'location' });
this.fetch();
},
setModelType: function(type) {
clearTimeout(this.timeout);
this.set({ model_type: type, update_type: 'model' });
this.fetch();
},
fetch: function() {
this.trigger('fetch', {});
}
});
var SearchController = function(model_options) {
var model = new Search(model_options);
var view = new SearchView({ model: model, el: $('#new_search') });
model.bind('fetch', function(model, response) {
_.each(response, function(selector, html) {
view.$('#' + selector).html(html);
});
});
this.model = model;
this.view = view;
};
var SearchView = Backbone.View.extend({
events: {
'click #search_model_filter a.search_model_link': 'selectModelType',
'click #search_recommendations_filter .search_link': 'changeRecommendationFrom'
},
initialize: function() {
this.tagsView = new SearchTagsView({ model: this.model, el: this.$('#search_tags_filter') });
this.modelView = new SearchModelView({ model: this.model, el: this.$('#search_model_filter') });
this.recommendationsView = new SearchRecommendationsView({ model: this.model, el: this.$('#search_recommendations_filter') });
}
});
var SearchTagsView = Backbone.View.extend({
events: {
'click a[data-id]': 'toggleTag',
'click a.show_more_tags': 'showMoreTags'
},
toggleTag: function(e) {
this.model.setSelectedTagIds(this.$('li.selected a').map(function(i,a) { return $(a).attr('data-id') } ).get().join(','));
this.$(e.currentTarget).closest('li').toggleClass('selected');
},
showMoreTags: function(e) {
this.$('li.hidden').removeClass('hidden');
}
});
var SearchModelView = Backbone.View.extend({
events: {
'click a.search_model_link': 'selectModelType'
},
selectModelType: function(e) {
var modelType = this.$(e.currentTarget).attr('href').replace('#', '');
this.model.setModelType(modelType);
this.$('a.search_model_link').removeClass('selected');
this.$(this).addClass('selected');
}
});
var SearchRecommendationsView = Backbone.View.extend({
events: {
'click a.search_link': 'selectRecommendationsType'
},
selectRecommendationsType: function(e) {
var recommendationsType = this.$(e.currentTarget).attr('href').replace('#', '');
this.model.setRecommendationsType(recommendationsType);
this.$('a.search_link').removeClass('selected');
this.$(this).addClass('selected');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment