SimbCo / brunch-with-marionette

Simple skeleton for brunch.io utilizing MarionetteJS, Handlebars, Stylus and Backbone
27 stars 10 forks source link

Marionette Modules #3

Closed gregtap closed 10 years ago

gregtap commented 10 years ago

It would be very interesting to see how to solve the use of Marionette modules in a separate file than application.coffee in a Brunch context.

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.module.md

MyApp.module "MyApp.ModuleA", (ModuleA, MyApp, Backbone, Marionette, $, _) ->
  ModuleA.addInitializer ->
    console.log "START MODULE A"
DorianListens commented 10 years ago

Is there any way to get this to work? It seems to be a problem with the fact that Brunch wraps everything as CommonJs modules, and that breaks Marionette's built in module system.

Any pointers?

gregtap commented 10 years ago

Hi Dorian, I could not find a proper way so I just crammed up stuff in one file: https://github.com/novapost/dragndoc/blob/master/app/application.coffee

DorianListens commented 10 years ago

Okay, I guess that's what I'll have to do for now then. Thanks for the quick reply! Hopefully this is something that'll be fixable in the future?

simb commented 10 years ago

Hey All,

So maybe I set things up differently than most so I'd love to get your feedback. In this skeletons application.coffee I export an instance of the application. Which means from anywhere you can do `app = require('application') and get the single instance of your application.
https://github.com/SimbCo/brunch-with-marionette/blob/master/app/application.coffee

So I have always kept all my modules in other files and I set them up something like this.

application = require 'application' Layout = require('./views/DataEntryLayout')

module.exports = application.module("DataEntry", ( mod, app, Backbone, Marionette, $, _ )=>

mod.startWithParent = false;

mod.addInitializer( (options) =>

    ViewController = require './controllers/ViewController'
    MainController = require './controllers/MainController'

    mod.viewController = new ViewController()
    mod.mainController = new MainController()

    mod.layout = new Layout()
    mod.layout.render()

    # Freeze the object
    #Object.freeze? this
)

mod.on("start", =>
    mod.viewController.dispatch("load:de_home")
)

mod.on('render', =>

)

mod.on('before:stop', =>
    application.model.set("userHasEditedRecord",false)
)

mod.addFinalizer( =>
    mod.viewController.stopListening()
    mod.mainController.stopListening()
    delete mod.viewController
    delete mod.mainController
)

)

I have some other stuff in there from a project I call Paddles which decouples the marionette controllers from the routing mechanism. But the point is to show that you can separate your modules right now.

gregtap commented 10 years ago

Nice trick, you are right it is possible.

simb commented 10 years ago

Ok, cool. Let me know if someone thinks of a way to improve this.