Can Accessor not be used as Singleton?

1222
2
Jump to solution
03-17-2017 12:26 PM
BenFousek
Occasional Contributor III

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;
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
deleted-user-iPdcWUgWcsoy
New Contributor III

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
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
deleted-user-iPdcWUgWcsoy
New Contributor III

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
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
BenFousek
Occasional Contributor III

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.