Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Technical details (also in application.js):
RequireJS docs: http://requirejs.org/docs/api.html
With AMD the file-name is the name of the module, so define it anonymously:
You can depend on scripts using the assets-pipeline name, and add a shim config in requirejs.yml if it is not using AMD. jQuery-plugins typically don't have to use exports. See requirejs docs for more info about shims: http://requirejs.org/docs/api.html#config-shim
This is the main script for requirejs on every page of the site. Inline script has been replaced with re-usable widgets. In development mode the widgets and dependencies will be loaded async on demand, but for production the new widgets have to be added to bundle.js manually.
Since a widget should be re-usable, it should not hard-code what elements to attach to. Instead it should return a function that takes an element as first argument, and initialize the widget on this element. Ideally a widget is just a thin layer to bind other modules to HTML-elements.
Sometimes you need to pass dynamic data to the widgets. Preferably the widget/module should load this through ajax instead, but it can also use a named define (bad practise):
The widget/module have to require the data on demand to avoid compilation errors when building the production bundle.
What do others think of this? Any feedback greatly appreciated :)
Cool! The build failed because these files contain tab characters: