1
votes

I'm trying to extend marionette's module class in coffeescript, using a commonJS pattern, so that I can require a general Module class to reuse across parts of my app. So far, no luck.

So far I've tried this:

module.exports = class SingleFeed extends Marionette.Module

    @addInitializer((options) =>
        console.log 'initialize'
    )

and this:

module.exports = Marionette.Module.extend(

    @addInitializer((options) =>
        console.log 'initialize'
    )

)

With the hope that I could reuse this code in my application like this:

HomeFeed = require '../modules/components/feeds/SingleFeed'
hf = new HomeFeed()

app.module('HomeFeed', 
     moduleClass: hf
).start(options)

Has anyone successfully done this with coffeescript? Or does anyone have any ideas that might help?

Here's the documentation I'm referencing: https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.module.md

1
The reason your attempts don't work aren't related to CoffeeScript: it's more like you're doing the right thing at the wrong time, as @dmytro-yarmak explains in his answer. To extend a CS class or marionette module you need to provide an extension object; instead, your passing it the result of calling a member function. Worse still, there's no instance yet (you're defining a class), so calling @addInitializer doesn't make sense.Esteban

1 Answers

1
votes

The problem that addInitializer is method of module instance but when you extended you can't call it. But you can override empty default initialize where you can add your initializers and finalizers like that:

module.exports = Marionette.Module.extend(
  initialize: () ->
    console.log('initialize module')
    @addInitializer((options) =>
        console.log 'starting module'
    )

)