Class Index | File Index

Classes


Class $class.descriptor.multiton_config

This is a fake class to document the configuration object for use with $class.descriptor#$multiton to setup a class as a multiton. If the defaults are acceptable, you can simply provide a value of true instead of a multiton config object.


Defined in: class-debug.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
Field Summary
Field Attributes Field Name and Description
 

A function that is used to create a cache key for looking up cached multiton instances.

 

A function that is used to create each multiton instance.

Class Detail
$class.descriptor.multiton_config()
Field Detail: createCacheKey
{Function} createCacheKey

A function that is used to create a cache key for looking up cached multiton instances. This function receives all arguments that are passed to the class's static getInstance method and is expected to return a String that uniquely identifies the combination of arguments. The arguments should be "normalized", such that if two different sets of arguments would be interpretted the same and result in equivalent instances, an identical cache key should be generated for those two different sets of arguments.

Defaults to a function that simply converts all arguments to Strings and joins them together with pipes (|).

Example:

$class("Example", {
  $multiton: {
    createCacheKey: function(name) {
      // the cache key must account for any kind of 
      // defaulting/processing of params in the constructor
      return name || "John Doe";
    }
  },
  
  // name defaults to "John Doe" if empty or null
  $constructor: function(name) {
    this._name = name || "John Doe";
  },
  
  getName() {
    return this._name;
  }
});

var example1 = Example.getInstance(null);
var example2 = Example.getInstance("John Doe");

// same name
alert(example1.getName() == example2.getName()) // alerts "true"
// same instance
alert(example1 == example2) // alerts "true"

Field Detail: createInstance
{Function} createInstance

A function that is used to create each multiton instance. This function receives all arguments that are passed to the class's static getInstance method and is expected to return an instance of the class. The function is called within the context of the class/constructor. Defaults to a function that simply instantiates the class with the same arguments that were passed to getInstance().

I'm having a hard time coming up with a legitemate use for this property, but it seemed like it should be supported to maintain consistency with $class.descriptor.singleton_config. Good luck :)


Documentation generated by JsDoc Toolkit 2.3.2 on Sat Nov 13 2010 01:18:48 GMT-0500 (EST)