f / delorean

An Agnostic, Complete Flux Architecture Framework
749 stars 40 forks source link

Doesn't work with Require.js #72

Open bradleyayers opened 9 years ago

bradleyayers commented 9 years ago

The bower distributed delorean does not work with Require.js

First problem is require('events').EventEmitter:

module.exports = requirements = {
  // DeLorean uses **Node.js native EventEmitter** for event emittion
  EventEmitter: require('events').EventEmitter,
  // and **es6-promise** for Deferred object management.
  Promise: require('es6-promise').Promise
};

But the module is defined as:

module.exports = EventEmitter

So I think in the injector it should just be require('event') right?

Second problem is in the injector:

// This library needs to work for Browserify and also standalone.
// If DeLorean is defined, it means it's called from the browser, not
// the browserify.
if (typeof DeLorean !== 'undefined') {
  for (var requirement in requirements) {
    DeLorean.Flux.define(requirement, requirements[requirement]);
  }
}

But when used in the browser with Require.js, DeLorean is undefined because:

if (typeof define === 'function' && define.amd) {
  define([], function () {
    return DeLorean;
  });
} else {
  window.DeLorean = DeLorean;
}
bradleyayers commented 9 years ago

I also tried building it with browserify, but get the same problem when using the build version in the browser.

bradleyayers commented 9 years ago

I can work around it by just use delorean.js directly (without requirements.js) from the npm distribution (because it's not yet browserified). I then define my own module delorean.flux that does the DeLorean.Flux.define setup:

define(["delorean", "eventemitter2", "es6-promise"], function (delorean, EventEmitter2, Promise) {
  delorean.Flux.define("EventEmitter", EventEmitter2);
  delorean.Flux.define("Promise", Promise);
  return delorean.Flux;
});