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

Doesn't work with browserify #66

Closed
adamscybot opened this issue Sep 3, 2013 · 4 comments
Closed

Doesn't work with browserify #66

adamscybot opened this issue Sep 3, 2013 · 4 comments
Assignees

Comments

@adamscybot
Copy link

Backbone associations can not be used in a browserify stack as both the window and exports modules are defined as objects at runtime on the clientside. For this reason, the require('backbone') etc calls are not made and so instead it assigns Backbone and _ to exports._ and exports.Backbone, which are by definition, empty. Presumably that branch of the if logic should only fire when root == window and never root == exports as it does now in a browserify environment.

(typeof exports === "object") instead of (typeof window === "undefined") resolves the issue. Basically the key is to give priority to checking exports over window first.

One way I've seen which seems to cover all the bases is:

  // CommonJS
  if (typeof exports == "object") {
    var _ = require("underscore"),
    Backbone = require("backbone");
    exports = module.exports = Backbone;
  }
  // Browser
  else if (typeof _ !== "undefined" &&
    typeof Backbone !== "undefined") {
    _ = root._;
    Backbone = root.Backbone;
  }
@ghost ghost assigned jdkanani Sep 3, 2013
@jdkanani
Copy link
Contributor

jdkanani commented Sep 4, 2013

Thanks @adamscybot for pointing this out. I have just pushed code to check exports over window as per your code snippet. Let me know if it works for you.

@adamscybot
Copy link
Author

Works correctly, thanks. I think the .min version needs updating though. Also a minor version push to NPM would be useful so I can stop pulling this repo directly in my package.json :).

Thanks again!

@dhruvaray
Copy link
Owner

Wonderful! Sure...In a couple of days at most. Don't forget to show your appreciation by starring us!

@dhruvaray
Copy link
Owner

Took longer than I thought, but v0.5.3 is now on npm!

@ghost ghost assigned dhruvaray Dec 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants