Last active
December 15, 2015 04:09
-
-
Save jurikern/5199636 to your computer and use it in GitHub Desktop.
Binded Backbone v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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