Created
January 4, 2013 15:35
-
-
Save daGrevis/4453516 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;(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