Created
October 31, 2012 20:40
-
-
Save lone-star/3989724 to your computer and use it in GitHub Desktop.
part1 for the bb-subset tutorial
This file contains 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
class Book extends Backbone.Model | |
class Books extends Backbone.Collection | |
model: Book | |
# Here we can pass json objects to fill the collection | |
books = new Books() |
This file contains 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
class List extends Backbone.View | |
el: '#list' | |
itemTemplate: _($('#item-template').text()).template() | |
initialize: -> | |
@collection.on('all', @render) | |
render: => | |
@$el.empty() | |
@collection.each(@insert) | |
insert: (book) => | |
@$el.append(@itemTemplate(book.toJSON())) |
This file contains 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
library = new Subset(source: books) |
This file contains 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
<div id="dashboard"> | |
<input name="title" type="text" placeholder="Title"/> | |
<input name="author" type="text" placeholder="Author"/> | |
<input name="categories" type="text" placeholder="Category"/> | |
</div> |
This file contains 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
class Dashboard extends Backbone.View | |
el: '#dashboard' | |
# More code ... |
This file contains 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
dashboard = new Dashboard(model: library) |
This file contains 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
el: '#dashboard' | |
events: | |
'keypress input[type=text]': 'filterOnEnter' | |
filterOnEnter: (event) -> | |
return if event.keyCode isnt 13 | |
$target = $(event.target) | |
@addFilter($target.attr('name'), $target.val()) | |
@clean() | |
clean: -> | |
@$('[type="text"]').val('') |
This file contains 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
addFilter: (attribute, val)-> | |
@collection.add | |
attribute: attribute | |
operator: @operators[attribute] or '==' | |
value: val.split(/[ ]*,[ ]*/) | |
operators: | |
title: 'in' | |
author: 'in' | |
categories: 'any' |
This file contains 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
<ul id="list" class="unstyled"></ul> |
This file contains 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
<script id="item-template" type="template/underscore"> | |
<li class="item"> | |
<h4><u><%=title%></u> by <%=author%></h4> | |
<h6 class="categories"><%=categories.join(', ')%></h6> | |
<hr/> | |
</li> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment