Skip to content

Instantly share code, notes, and snippets.

@fxposter
Created July 12, 2011 08:06
Show Gist options
  • Save fxposter/1077588 to your computer and use it in GitHub Desktop.
Save fxposter/1077588 to your computer and use it in GitHub Desktop.
var Tag, activeTags, inactiveTags, suggestedTags;
Application.Models.Tag = Backbone.Model.extend({});
Application.Models.Business.Tag = Application.Models.Tag.extend({
activate: function() {
var manager = this.get('manager');
if (!manager) return false;
manager.activateTag(this);
},
deactivate: function() {
var manager = this.get('manager');
if (!manager) return false;
manager.deactivateTag(this);
}
});
Application.Controllers.Businesses.Tags = Backbone.Router.extend({
routes: {
"": "active",
"inactive": "inactive",
"suggested": "suggested"
},
bootstrap: function() {
this.tagManager = new Application.Models.Business.TagManager();
this.tagManager.fetch();
this.menu = new MenuView({ model: this.tagManager, el: $('.tags_menu') });
this.menu.render();
},
active: function() {
var view = new TagsView({ models: this.tags.active, el: $('.features_list') });
view.render();
this.menu.setCurrent('active').render();
},
inactive: function() {
var view = new TagsView({ models: this.tags.inactive, el: $('.features_list') });
view.render();
this.menu.setCurrent('inactive').render();
},
suggested: function() {
var view = new TagsView({ models: this.tags.suggested, el: $('.features_list') });
view.render();
this.menu.setCurrent('suggested').render();
},
})
Application.Views = {};
var activateTag = function(tag) {
tag.activate();
activeTags.add(tag);
inactiveTags.remove(tag);
suggestedTags.remove(tag);
};
var deactivateTag = function(tag) {
tag.deactivate();
activeTags.remove(tag);
suggestedTags.remove(tag);
inactiveTags.add(tag);
};
var TagManager = Backbone.Model.extend({
url: '/business/id/features',
set: function(attributes, options) {
this.activeTags.reset()
Backbone.Model.prototype.set.call(this, attributes, options);
...
}
});
var TagView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "render");
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
var TagsView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "render");
},
render: function() {
$(this.el).empty();
_.each(function(tag) {
var tagView = new TagView({ model: tag });
$(this.el).append(tagView.render().el);
})
return this;
}
});
var MenuView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, "render");
var self = this;
this.get('model').bind('changed', function() { self.render(); });
},
render: function() {
$(this.el).empty();
_.each(function(tag) {
var tagView = new TagView({ model: tag });
$(this.el).append(tagView.render().el);
})
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment