index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. /**
  3. * @fileOverview This is base definition for all composed classes defined by the system
  4. * @module base
  5. * @requires base/chains
  6. * @requires dcl
  7. */
  8. var dcl = require( "dcl" );
  9. var chains = require( "./chains" );
  10. /**
  11. * @classdesc The base of all classes in the system, this is one of the few pure "classes" in core the of the system. It is a
  12. * pretty clean little class whose primary purpose is to surface the composition chains and a basis for storing
  13. * options on mixin and subclass instances. Options are handled at the instance rather than the prototype level
  14. * so that multiple instances don't compete for default values.
  15. *
  16. * @exports base
  17. * @constructor
  18. * @extends base/chains
  19. */
  20. var Base = dcl( [chains], /** @lends base# */{
  21. declaredClass : "Base",
  22. /**
  23. * Add an option to a class. If any members of the hash already exist in `this.options`, they will be overwritten.
  24. * @param {hash} options A hash of options you want to set
  25. * @see {base#addDefaultOptions}
  26. */
  27. addOptions : function ( options ) {
  28. options = options || {};
  29. if ( this.options ) {options = sys.extend( {}, sys.result( this, 'options' ), options );}
  30. this.options = options;
  31. },
  32. /**
  33. * Add a default option to a class. The default options are only set if there is not already a
  34. * value for the option.
  35. * @param {hash} options A hash of options you want to set
  36. * @see {base#addOptions}
  37. */
  38. addDefaultOptions : function ( options ) {
  39. options = options || {};
  40. if ( this.options ) {options = sys.defaults( {}, sys.result( this, 'options' ), options );}
  41. this.options = options;
  42. },
  43. /**
  44. * Call this to close your object and dispose of all maintained resources. You can define this method on your
  45. * own classes without having to call the superclass instance, however it is reccomended that you put
  46. * all disposal code in `destroy()`. You must be disciplined about calling this on your instances.
  47. * @see {base/chains#end}
  48. * @see {base/chains#destroy}
  49. */
  50. end : function () {
  51. this.destroy()
  52. },
  53. /**
  54. * Called when it is time to get rid of all of your instance level references and objects and events. You can
  55. * define this method on your own classes without having to call the superclass instance. It is called by
  56. * `instance.end()` automatically
  57. * @see {base/chains#end}
  58. * @see {base/chains#destroy}
  59. */
  60. destroy : function () {
  61. }
  62. } );
  63. Base.compose = dcl;
  64. Base.mixin = dcl.mix;
  65. module.exports = Base;