Last active
December 18, 2015 05:38
-
-
Save mbajoras/5733771 to your computer and use it in GitHub Desktop.
Marionette.js module for my blog article about Marionette.js stateful views.
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
# Initializes Marionette.js global application object. | |
@gApp = new Backbone.Marionette.Application() | |
# Prepares the DOM by assigning regions to various elements. | |
@gApp.addInitializer (options) -> | |
@addRegions(content: 'body') | |
# Login page controller. | |
@gApp.module 'LoginPage', (module, app, backbone, marionette, $, _) -> | |
module.addInitializer (options) -> | |
module.loginModel = new ALoginModel() | |
module.loginView = new ALoginView(model: module.loginModel) | |
app.content.show(module.loginView) | |
# Called when the async request to the server returns a successful status. | |
module.loginSuccess = (data) -> | |
module.loginModel.set('state', module.loginModel.authSuccessState) | |
# Called when the async request to the server returns an unsuccessful status. | |
module.loginFail = (response) -> | |
# HTTP 404 means that the user + password combo was not found. | |
# This is the "incorrect username/password" error. | |
# Any other status code indicates something unexpected happened on the | |
# server such as HTTP 418: I'm a Teapot. | |
if 404 == response.status | |
module.loginModel.set('state', module.loginModel.authFailState) | |
else | |
module.loginModel.set('state', module.loginModel.authUnknownState) | |
module.loginModel.set('stateDetails', 'Unexpected Server Response: ' + | |
response.status + ' ' + response.statusText) | |
# The view fires off a global event when the form is submitted so that this | |
# controller can catch it and handle the server communication logic. | |
app.vent.on 'login:submit', (loginModel) => | |
loginModel.set('state', loginModel.pendingAuthState) | |
$.post('/api/auth', loginModel.toJSON()) | |
.done((data) => module.loginSuccess(data)) | |
.fail((response) => module.loginFail(response)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A good module example