Skip to content

Instantly share code, notes, and snippets.

@plusjade
Created August 30, 2013 22:15
Show Gist options
  • Save plusjade/6394822 to your computer and use it in GitHub Desktop.
Save plusjade/6394822 to your computer and use it in GitHub Desktop.
// model View
Gild.Views.ProfileTag = Backbone.View.extend({
model: Gild.Models.Tag,
tagName: "li",
initialize: function(attrs) {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.remove, this);
},
template: '<a href="#" class="tag">{{ name }}</a><a href="#" class="remove" title="remove this tag">x</a>',
events: {
"click a.remove" : "promptDestroy"
},
render: function() {
return this.$el
.html(Mustache.render(this.template, this.model.attributes))
.find('a.tag')
.popover({
placement: 'bottom',
title: 'Added by',
content: this.model.get('added_by') + ' on ' + this.model.get('human_created_at')
})
.hover(function(){ $(this).popover('show') }, function(){ $(this).popover('hide') })
.end();
},
promptDestroy: function(e){
var model = this.model;
Gild.utils.promptDeleteResource.show({
text : model.get('name'),
remove: function(){
model.destroy()
}
})
e.preventDefault();
return false;
}
});
// Collection view
Gild.Views.ProfileTags = Backbone.View.extend({
model : Gild.Models.Tag,
collection : Gild.Collections.Tags,
template: '<div class="profile-list-tags"><ul class="tag_box"><li class="header-icon"><i class="icon-tags"></i></li></ul></div>',
initialize : function(){
this.$el = this.$el.find('.footer').prepend(this.template).find('ul').first();
this.collection.on("reset", this.render, this);
this.collection.on("add", this.renderOne, this);
this.collection.on("add", this.toggleVisibility, this);
this.collection.on("remove", this.toggleVisibility, this);
this.collection.on("remove", this.notifyRemove, this);
},
render : function() {
var content = [];
this.collection.each(function(model){
content.push(new Gild.Views.ProfileTag({ model : model }).render());
})
$.fn.append.apply(this.$el, content);
this.toggleVisibility();
},
renderOne : function(tag){
this.$el.append(new Gild.Views.ProfileTag({model : tag }).render());
},
toggleVisibility : function() {
if(this.collection.length === 0)
this.$el.parent().hide();
else
this.$el.parent().show();
},
notifyRemove : function(tag) {
Gild.utils.notify({
status: "good",
message: 'Tag "' + tag.get('name') + '" Removed'
}, { primary: true });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment