Skip to content

Instantly share code, notes, and snippets.

@jurikern
Last active December 15, 2015 04:09
Show Gist options
  • Save jurikern/5199636 to your computer and use it in GitHub Desktop.
Save jurikern/5199636 to your computer and use it in GitHub Desktop.
Binded Backbone v2
class Application.Views.Base
@bindWith: (selector) ->
instance = @
$(selector).each ->
new instance($(@))
constructor: (@container) ->
_.extend(@, Backbone.Events)
class Application.Views.Users.Login extends Application.Views.Base
constructor: (@container) ->
super(@container)
@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.bindWith("*[data-block=login_form]")
jQuery ->
Application.Views.Users.Login.bindWith("*[data-block=login_form]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment