Skip to content

Instantly share code, notes, and snippets.

@lone-star
Created October 31, 2012 20:40
Show Gist options
  • Save lone-star/3989724 to your computer and use it in GitHub Desktop.
Save lone-star/3989724 to your computer and use it in GitHub Desktop.
part1 for the bb-subset tutorial
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()
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()))
library = new Subset(source: books)
<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>
class Dashboard extends Backbone.View
el: '#dashboard'
# More code ...
dashboard = new Dashboard(model: library)
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('')
addFilter: (attribute, val)->
@collection.add
attribute: attribute
operator: @operators[attribute] or '=='
value: val.split(/[ ]*,[ ]*/)
operators:
title: 'in'
author: 'in'
categories: 'any'
<ul id="list" class="unstyled"></ul>
<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