Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Created January 4, 2013 15:35
Show Gist options
  • Select an option

  • Save daGrevis/4453516 to your computer and use it in GitHub Desktop.

Select an option

Save daGrevis/4453516 to your computer and use it in GitHub Desktop.
;(function($) {
$(function() {
"use strict"
var bookmarks = [
{url: "http://google.com/", title: "Google.com"},
{url: "http://dagrevis.lv/", title: "daGrevis.lv"}
]
var Bookmark = Backbone.Model.extend({
defaults: {
"times_visited": 0
}
})
var Directory = Backbone.Collection.extend({
model: Bookmark
})
var BookmarkView = Backbone.View.extend({
tagName: "div",
className: "single_bookmark",
template: $("#single_bookmark").html(),
render: function() {
var template = _.template(this.template)
this.$el.html(template(this.model.toJSON()))
return this
}
})
var DirectoryView = Backbone.View.extend({
el: $("#bookmarks"),
initialize: function() {
this.collection = new Directory(bookmarks)
this.render()
},
render: function() {
var that = this
_.each(this.collection.models, function(item) {
that.render_bookmark(item)
})
},
render_bookmark: function(item) {
var bookmark_view = new BookmarkView({
model: item
})
this.$el.append(bookmark_view.render().el)
}
})
new DirectoryView()
})
}(jQuery))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment