gouthamvel
4/17/2012 - 6:39 PM

router.js.coffee

class Deliveryguy.Routers.PostsRouter extends Backbone.Router
  initialize: (options) ->
    @posts = new Deliveryguy.Collections.PostsCollection()
    @posts.reset options.posts

  routes:
    "/new"      : "newPost"
    "/index"    : "index"
    "/:id/edit" : "edit"
    "/:id"      : "show"
    ".*"        : "index"

  newPost: ->
    @view = new Deliveryguy.Views.Posts.NewView(collection: @posts)
    $("#posts").html(@view.render().el)

  index: ->
    @view = new Deliveryguy.Views.Posts.IndexView(posts: @posts)
    $("#posts").html(@view.render().el)

  show: (id) ->
    post = @posts.get(id)

    @view = new Deliveryguy.Views.Posts.ShowView(model: post)
    $("#posts").html(@view.render().el)

  edit: (id) ->
    post = @posts.get(id)

    @view = new Deliveryguy.Views.Posts.EditView(model: post)
    $("#posts").html(@view.render().el)