Skip to content

Instantly share code, notes, and snippets.

@andrewhowdencom andrewhowdencom/factory.js
Last active Mar 15, 2016

Embed
What would you like to do?
factory.js
/**
* Copyright (c) 2015 littleman.co
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* A skeleton function for defining a JS module. Benefits
*
* - It's dependency manager friendly
* - It keeps things of a similar responsiblity in the same place, and encourages you to think about a (programmatic) API with a JS Moduile
* - If you concatenate your JS (ewww. Use H/2) it keeps everything from exploding, and allows you to use 'use strict'.
*/
(function(root, definition) {
'use strict';
if (typeof module === 'object' && module.exports && typeof require === 'function') {
// Common JS
module.exports = definition();
// ^^ The above has absolutely never been tested. I'm not even sure how dependencies are declared.
// AMD, Require.js
} else if (typeof define === 'function' && typeof define.amd === 'object') {
define(function(require) {
var $ = require('jQuery'); // An example dependency
return definition($)
});
// If there are no dependencies, `define(definition);` works just fine.
} else {
// Expose the global here; // Todo: You should rename this to something logical. Also, it's not a really
// good idea to pollute the global namespace.
root.obj = definition();
}
}(this, function($) {
'use strict';
var noop = function() {};
var obj = {};
// Check if the dependency has loaded. A dependency will exist within whatever scope it's defined (currently,
// my understanding is the "best" method is within an AMD closure).
if (typeof $ === 'undefined') {
return noop;
}
return obj;
}));
/**
* An example function factory implementation with a jQuery plugin. Also includes some notes on jQuery best practices
*/
(function(root, definition) {
if (typeof module === 'object' && module.exports && typeof require === 'function') {
// Common JS
module.exports = definition();
} elseif (typeof define === 'function' && typeof define.amd === 'object') {
define(function(require) {
var $ = require('jQuery');
return definition($);
})
}
'use strict';
})(this, function($) {
'use strict';
var noop = function() {};
if (typeof $ === 'undefined') {
return noop();
}
// This isn't very good yet, as there's nothing here! Shock horror. Later, there'll be some jQuery specific stuff.
// Todo: Check a plugin exists
// Todo: allow disabling the plugin
return $;
});
@andrewhowdencom

This comment has been minimized.

Copy link
Owner Author

commented Nov 10, 2015

Todo:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.