Skip to content

Instantly share code, notes, and snippets.

@basketofsoftkittens
Last active December 20, 2015 09:30
Show Gist options
  • Save basketofsoftkittens/6108257 to your computer and use it in GitHub Desktop.
Save basketofsoftkittens/6108257 to your computer and use it in GitHub Desktop.
singleton for requirejs
_.mixin({
singleton:function(Constructor, scope) {
var instance;
return function(settings) {
var args = _.toArray(arguments);
var wrapper = function(f, params) {
var params = [f];
params = params.concat(args);
return f.bind.apply(f, params);
};
if (_.isObject(settings) && settings.singleton && instance) {
return instance;
} else {
instance = new (wrapper(Constructor, args));
return instance;
}
};
}
});
define(['module'], function(module) {
return _.singleton(module);
});
@basketofsoftkittens
Copy link
Author

downside is that you have to have es5 bind and i dont know how to release the models from memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment