Last active
December 20, 2015 09:30
-
-
Save basketofsoftkittens/6108257 to your computer and use it in GitHub Desktop.
singleton for requirejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
downside is that you have to have es5 bind and i dont know how to release the models from memory