2

Using Chaplin with Browserify and jQuery requires you to do the following:

Backbone = require('backbone')
$ = require('jquery')
Backbone.$ = $
Chaplin = require('chaplin')

This must be written EVERY TIME you require('chaplin'). If you miss this even once in any module that uses Chaplin and if that module is initialized first, then Chaplin will be broken because it will initialize to using Chaplin without jQuery, but later you end up setting Backbone.$ to jQuery and Chaplin is not expecting that.

An example of what this will break is Chaplin's View which will be initialized to using 'appendChild' instead of 'append' for containerMethod. But the element will be a jQuery selector which doesn't have appendChild.

Is there any way to force the order in Browserify so that this boilerplate code isn't required in every single module that uses Chaplin?

joews
  • 29,767
  • 10
  • 79
  • 91
donaddon
  • 413
  • 2
  • 13

1 Answers1

3

You could create a module that runs your init steps and exports Chaplin.

// my-chaplin.js
var Backbone = require('backbone')
Backbone.$ = require('jquery')
module.exports = require('chaplin')

Always require this module instead of requiring chaplin directly:

// Each module in your app
var Chaplin = require('./my-chaplin');

CommonJS guarantees that this initialisation runs once, no matter how many times you require it.

Community
  • 1
  • 1
joews
  • 29,767
  • 10
  • 79
  • 91
  • Yeah, that would work. Instead of updating all the modules and counting on our engineers to remember to include my-chaplin, we've decided to take that module and force it to be the first one on the Browserify command line. Not sure if that's guaranteed, but it's working now. It would be nice if browserify had a declarative way of manipulating order. – donaddon Nov 12 '15 at 19:22
  • I manage it by having browserify process a single root file that requires the rest of the app. Any up front config happens there. Can you do that rather than passing several files to browserify? – joews Nov 12 '15 at 20:56