Skip to content

Instantly share code, notes, and snippets.

@jurikern
Created March 18, 2013 22:41
Show Gist options
  • Save jurikern/5191521 to your computer and use it in GitHub Desktop.
Save jurikern/5191521 to your computer and use it in GitHub Desktop.
Login with binded backbone
class Application.Views.Users.Login
@bindedWith: (selector) ->
$(selector).each ->
Application.Views.Users.Login($(@))
constructor: (@container) ->
_.extend(@, Backbone.Events)
@initForm()
initForm: =>
@form = @container.find("form")
@form.on "ajax:success", @createResponse
@form.on "request:completed", @completeForm
@form.on "request:failed", @updateForm
@submitButton = @form.find("a[data-action=submit]")
@submitButton.on "click", =>
@form.trigger("submit.rails")
createResponse: (event, json) =>
if json["completed"] == true
@form.trigger("request:completed", json)
else
@form.trigger("request:failed", json)
completeForm: (event, json) =>
console.log(json)
updateForm: (event, json) =>
@container.replaceWith(json["template"])
Application.Views.Users.Login.bindedWith("*[data-block=login_form]")
jQuery ->
Application.Views.Users.Login.bindedWith("*[data-block=login_form]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment