Skip to content

Instantly share code, notes, and snippets.

@mbajoras
Last active December 18, 2015 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbajoras/5733771 to your computer and use it in GitHub Desktop.
Save mbajoras/5733771 to your computer and use it in GitHub Desktop.
Marionette.js module for my blog article about Marionette.js stateful views.
# 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))
@charlycoste
Copy link

A good module example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment