From 2ee1069e32f750538b7d2bc31b14a96d3c4a7002 Mon Sep 17 00:00:00 2001 From: Ben Haines Date: Thu, 16 Jan 2014 17:22:25 +1300 Subject: [PATCH 1/2] Add support for RequireJS, AMD modules and a global Backbone.Form object in one file. --- scripts/build-templates/main.js | 42 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/scripts/build-templates/main.js b/scripts/build-templates/main.js index a655995c..9a8b85f9 100644 --- a/scripts/build-templates/main.js +++ b/scripts/build-templates/main.js @@ -6,34 +6,30 @@ * License and more information at: * http://github.com/powmedia/backbone-forms */ -;(function(root) { - - //DEPENDENCIES - //CommonJS - if (typeof exports !== 'undefined' && typeof require !== 'undefined') { - var $ = root.jQuery || root.Zepto || root.ender || require('jquery'), - _ = root._ || require('underscore'), - Backbone = root.Backbone || require('backbone'); - } - - //Browser - else { - var $ = root.jQuery, - _ = root._, - Backbone = root.Backbone; +(function(root, factory) { + + // Set up Backbone.Form appropriately for the environment. Start with AMD. + if (typeof define === 'function' && define.amd) { + define(['backbone', 'underscore', 'jquery', 'exports'], function(Backbone, _, $, exports) { + exports = factory(root, _, Backbone, $); + }); + + // Next for Node.js or CommonJS. jQuery may not be needed as a module. + } else if (typeof exports !== 'undefined') { + var _ = require('underscore'), Backbone = require('backbone'), $; + try { $ = require('jquery'); } catch(e) {} + module.exports = exports = factory(root, _, Backbone, $); + + // Finally, as a browser global. + } else { + root.Backbone.Form = factory(root, root._, root.Backbone, (root.jQuery || root.Zepto || root.ender || root.$)); } +}(this, function(root, _, Backbone, $) { //SOURCE {{body}} - //Metadata Form.VERSION = '{{version}}'; - - - //Exports - Backbone.Form = Form; - if (typeof exports !== 'undefined') exports = Form; - -})(window || global || this); +})); From aeb52caf37b65d07c833016a2da9e15c5efbce4c Mon Sep 17 00:00:00 2001 From: Ben Haines Date: Thu, 16 Jan 2014 18:04:20 +1300 Subject: [PATCH 2/2] Whoops! Add the return. --- scripts/build-templates/main.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/build-templates/main.js b/scripts/build-templates/main.js index 9a8b85f9..93150146 100644 --- a/scripts/build-templates/main.js +++ b/scripts/build-templates/main.js @@ -32,4 +32,6 @@ //Metadata Form.VERSION = '{{version}}'; + + return Form; }));