Skip to content

Instantly share code, notes, and snippets.

@moski
Created November 28, 2011 12:15
Show Gist options
  • Save moski/1400188 to your computer and use it in GitHub Desktop.
Save moski/1400188 to your computer and use it in GitHub Desktop.
A sample New View backbone class that uses iframe.
Lifelane.Views.Posts ||= {}
class Lifelane.Views.Posts.NewView extends Backbone.View
template: JST["backbone/templates/posts/new"]
events:
"submit #create-post": "save"
# The constructor for creating new post.
constructor: (options) ->
super(options)
@model = new @collection.model()
@model.bind("change:errors", () => this.render())
# Saving a new post.
# options:
# success
# error
# iframe
#
save: (e) ->
e.preventDefault()
e.stopPropagation()
@model.unset("errors")
@collection.create(@model.toJSON(),
success: (post) =>
if(post.isNew())
@model.set({errors: post})
else
@model = post
window.location.hash = "/index"
error: (post, jqXHR) =>
@model.set({errors: $.parseJSON(jqXHR.responseText)})
iframe: true
)
render: ->
$(this.el).html(@template(@model.toJSON() ))
this.$("form").customBackboneLink(@model)
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment