Collections in Spine.js

http://stackoverflow.com/questions/9611318/collections-in-spine-js

by SpoBo

HTML

<script src="https://raw.github.com/maccman/spine/master/lib/spine.js"></script>
<script src="https://raw.github.com/maccman/spine/master/lib/ajax.js"></script>
<div id="app">
    
</div>

CoffeeScript

# -- Spine app:
App = {}

class App.Post extends Spine.Model
    @configure 'Post', 'name', 'published_at'
    
    isPublished: ->
        @published_at < new Date

class App.PostCollection extends App.Post

    @published: ->        
        @select (post) -> post.isPublished()
            

class App.Bootstrap extends Spine.Controller

  constructor: ->
    super           
    @log 'initialized'

      
# -- Browser:
# Instantiate controller.
window.app = new App.Bootstrap(el: $('#app'))
      
# Add some posts
yesterday = new Date
yesterday.setDate(yesterday.getDate()-1)
App.Post.create(name: 'first!!!11!!', published_at: yesterday)
      
tomorrow = new Date
tomorrow.setDate(tomorrow.getDate()+1)
App.Post.create(name: 'future :o', published_at: tomorrow)
      
console.log 'all:', App.PostCollection.all()
console.log 'published:', App.PostCollection.published()