Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##class Nfproto.Controllers.PostsController extends Backbone.Controller
- routes:
- "posts/:id": "edit"
- "": "index"
- new: "newDoc"
- edit: (id) ->
- post = new Post(id: id)
- post.fetch
- success: (model, resp) ->
- new Nfproto.Views.Posts.EditView(model: post)
- error: ()->
- new Error(message: "Could not find that post.")
- window.location.hash = "#"
- index:() ->
- $.getJSON "/posts", (data) ->
- if data
- posts = _(data).map((i) ->
- new Nfproto.Models.Post(i)
- )
- new Nfproto.Views.Posts.IndexView(posts: posts)
- else
- new Error(message: "Error loading posts.")
- newDoc: ->
- new Nfproto.Views.Posts.EditView(model: new Nfproto.Models.Post())
- ## class Nfproto.Views.Posts.EditView extends Backbone.View
- events: "submit form": "save"
- initialize:() ->
- @render()
- save:() ->
- self = this
- msg = (if @model.isNew() then "Successfully created!" else "Saved!")
- @model.save
- title: @$("[name=title]").val()
- content: @$("[name=content]").val()
- ,
- success: (model, resp) ->
- self.model = model
- self.render()
- self.delegateEvents()
- Backbone.history.saveLocation "posts/" + model.id
- false
- render:() ->
- out = "<form>"
- out += "<label for='title'>Title</label>"
- out += "<input name='title' type='text' />"
- out += "<label for='content'>content</label>"
- out += "<textarea name='content'>" + (@model.escape("content") or "") + "</textarea>"
- submitText = (if @model.isNew() then "Create" else "Save")
- out += "<button>" + submitText + "</button>"
- out += "</form>"
- $(@el).html out
- $("#app").html @el
- @$("[name=title]").val @model.get("title")
- ## When going to the newDoc edit view
- error: func is undefined
- at:
- _.bind = function(func, obj) {
- if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
Add Comment
Please, Sign In to add comment