I'm playing around with Accessor and trying to return a Singleton but requiring the module anytime after first require on booting the app just returns an Object.
// returns Singleton
define([
'dojo/_base/declare',
'dojo/Stateful',
'dojo/Evented'
], function (
declare,
Stateful,
Evented
) {
var _instance = null;
var Model = declare([Stateful, Evented], {
// stuff
});
if (!_instance) {
_instance = new Model();
}
return _instance;
});
// returns Object
define([
'esri/core/Accessor',
'dojo/Evented'
], function (
Accessor,
Evented
) {
var _instance = null;
var Model = Accessor.createSubclass([Evented], {
// stuff
});
if (!_instance) {
_instance = new Model();
}
return _instance;
});
Solved! Go to Solution.
Perhaps you find further informations in this link for real singletons (entry by "JonS"). We use it in our applications.
http://stackoverflow.com/questions/4334437/dojo-singleton-or-at-least-static-method-variable
Your second sample code can look like this.
// returns Object
define([
'esri/core/Accessor',
'./Singleton',
'dojo/Evented'
], function (
Accessor,
Evented
) {
var Model = Singleton(Accessor.createSubclass([Evented], {
// stuff
}));
return Model.getInstance(); // or just return the singleton
});
Perhaps you find further informations in this link for real singletons (entry by "JonS"). We use it in our applications.
http://stackoverflow.com/questions/4334437/dojo-singleton-or-at-least-static-method-variable
Your second sample code can look like this.
// returns Object
define([
'esri/core/Accessor',
'./Singleton',
'dojo/Evented'
], function (
Accessor,
Evented
) {
var Model = Singleton(Accessor.createSubclass([Evented], {
// stuff
}));
return Model.getInstance(); // or just return the singleton
});
Thanks for the reply Martin. That's more elegant.
My problem was configuring dojo and booting the app in the same require(). Dojo didn't like the relative path when requiring the model after boot.
Moved dojo config to its own require to fix problem.