Skip to content

Instantly share code, notes, and snippets.

@dey-dey
Created January 14, 2014 15:24
Show Gist options
  • Save dey-dey/8420032 to your computer and use it in GitHub Desktop.
Save dey-dey/8420032 to your computer and use it in GitHub Desktop.
Referencing path alias and path to said file from different modules causes anonymous define function to run twice :/
// create path for class.js
require.config({
paths: {
vvImptSingleton: 'path/to/veryImptSingleton'
}
});
// file: path/to/veryImptSingleton
// srsly important self-serious singleton
define(function (){
var VeryVeryImportantSingleton = function(){ ... }
VeryVeryImportantSingleton.prototype = {
//...shared methods
}
console.log('outside!')
//IIF closure over instance variable
return function() {
var instance = null;
return function(){
console.log('inside!', instance);
if (instance === null) {
instance = new VeryVeryImportantSingleton();
}
return instance
}
}();
});
define(function(){
var singleton = require('vvImptSingleton');
singleton();
// console logs:
// outside!
// inside! null
});
define(function(){
var singleton = require('path/to/very-very-impt-singleton');
singleton();
// console logs:
// outside!
// inside! null
});
// :/ wtf i am referencing the same file but it's executing
// the anonymous define function twice
// the obvious fix is to either reference path to file or the path alias
// but still should this be behavior? i couldn't see it in the documentation,
// but if it isn't there, should people know about this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment