Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing 'marionette.module' causes an error in the Marionette 3.0 build #6

Closed
d3chapma opened this issue Oct 19, 2018 · 4 comments
Closed

Comments

@d3chapma
Copy link

I know I'm late on this, but I'm trying to upgrade my app and I'm still on 2.4. I wanted to use the module shim to make this a bit easier, but if I add the line require('marionette.module').shim(Marionette) to my app, or even just import Module from 'marionette.module' then I get the following error:

Uncaught TypeError: Cannot read property 'triggerMethod' of undefined

It points to this code:

// Extend the Module prototype with events / listenTo, so that the module
// can be used as an event aggregator or pub/sub.
_.extend(Module.prototype, Backbone.Events, {

  // By default modules start with their parents.
  startWithParent: true,

  // Initialize is an empty function by default. Override it with your own
  // initialization logic when extending Marionette.Module.
  initialize: function() {},

  // Initializer for a specific module. Initializers are run when the
  // module's `start` method is called.
  addInitializer: function(callback) {
    this._initializerCallbacks.add(callback);
  },

  // Finalizers are run when a module is stopped. They are used to teardown
  // and finalize any variables, references, events and other code that the
  // module had set up.
  addFinalizer: function(callback) {
    this._finalizerCallbacks.add(callback);
  },

  // Start the module, and run all of its initializers
  start: function(options) {
    // Prevent re-starting a module that is already started
    if (this._isInitialized) { return; }

    // start the sub-modules (depth-first hierarchy)
    _.each(this.submodules, function(mod) {
      // check to see if we should start the sub-module with this parent
      if (mod.startWithParent) {
        mod.start(options);
      }
    });

    // run the callbacks to "start" the current module
    this.triggerMethod('before:start', options);

    this._initializerCallbacks.run(options, this);
    this._isInitialized = true;

    this.triggerMethod('start', options);
  },

  // Stop this module by running its finalizers and then stop all of
  // the sub-modules for this module
  stop: function() {
    // if we are not initialized, don't bother finalizing
    if (!this._isInitialized) { return; }
    this._isInitialized = false;

    this.triggerMethod('before:stop');

    // stop the sub-modules; depth-first, to make sure the
    // sub-modules are stopped / finalized before parents
    _.invoke(this.submodules, 'stop');

    // run the finalizers
    this._finalizerCallbacks.run(undefined, this);

    // reset the initializers and finalizers
    this._initializerCallbacks.reset();
    this._finalizerCallbacks.reset();

    this.triggerMethod('stop');
  },

  // Configure the module with a definition function and any custom args
  // that are to be passed in to the definition function
  addDefinition: function(moduleDefinition, customArgs) {
    this._runModuleDefinition(moduleDefinition, customArgs);
  },

  // Internal method: run the module definition function with the correct
  // arguments
  _runModuleDefinition: function(definition, customArgs) {
    // If there is no definition short circut the method.
    if (!definition) { return; }

    // build the correct list of arguments for the module definition

    // make flattening consistent
    // in lodash (v3) it's flatten(collection, deep)
    // and in underscore it's flatten(collection, shallow)
    var deepFlag = !_.flattenDeep;
    var args = _.flatten([
      this,
      this.app,
      Backbone,
      Marionette,
      Backbone.$, _,
      customArgs
      ], deepFlag);

    definition.apply(this, args);
  },

  // Internal method: set up new copies of initializers and finalizers.
  // Calling this method will wipe out all existing initializers and
  // finalizers.
  _setupInitializersAndFinalizers: function() {
    this._initializerCallbacks = new Callbacks();
    this._finalizerCallbacks = new Callbacks();
  },

  // import the `triggerMethod` to trigger events with corresponding
  // methods if the method exists
  triggerMethod: Marionette.triggerMethod // THIS IS THE LINE THAT THE STACK TRACE STARTS AT
});

Any ideas what is going on here?

@d3chapma d3chapma changed the title importing 'marionette.module' causes an error in the Marionette 3.0 build Importing 'marionette.module' causes an error in the Marionette 3.0 build Oct 19, 2018
@d3chapma
Copy link
Author

Seems like if I add a window.Marionette = Marionette before doing require('marionette.module').shim(Marionette) it works fine

@d3chapma
Copy link
Author

I should clarify that it fixes the issues with triggerMethod, but there seem to be other issues after that.

@paulfalgout
Copy link
Member

you probably have to expose Backbone like that as well. This is a pretty old shim, so I'd expect it would want everything available on window.

@d3chapma
Copy link
Author

Thanks for the response. Backbone is already exposed, so I'm not sure what is going on. It isn't a huge deal. I know there isn't huge incentive for you to support this shim. I was just curious if you had a easy solution in your back pocket. I'll just convert the last of our code that is using App.module to use es6 modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants