Extjs - A smart registry to find objects easy


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * @class App.Quick
  3.  * Special Class (Singleton)
  4.  * This small singleton works like a kind of application telephone book.
  5.  * A quick register to find critical and often used objects in the application.
  6.  * This object is in NO WAY generic and always application DEPENDENT !!!
  7.  *
  8.  * What makes this small class useful is that it dynamically generates the functions for how the
  9.  * values can be retrieved
  10.  *
  11.  * f.e.
  12.  *
  13.  * App.Quick.set('abba', this);
  14.  *
  15.  * Means that in the register entry abba the reference is set to "this", but in such a way that it
  16.  * will be set by reference, not by value!.
  17.  *
  18.  * You can retrieve this value simply by using:
  19.  * App.Quick.abba();
  20.  *
  21.  * App.Quick.check('abba');
  22.  * looks for the existance of a function called 'abba', used for validating if object functions already
  23.  * exist, before calling them.
  24.  *
  25.  * You can also create the entries directly (hard coded) in this class (sometimes good for overview)
  26.  *
  27.  * @namespace App
  28.  *
  29.  * @singleton
  30.  * @author J.J. van de Merwe, Enovision GmbH
  31.  * @version 1.0
  32.  */
  33.  
  34. Ext.ns('App');
  35.  
  36. App.Quick = function(){
  37.  
  38. return {
  39.  
  40. settings : {},
  41.  
  42. /**
  43.   * function that checks if an entry already been created for this class (dynamically that is)
  44.   * @params {string} set, which contains the label of the set item to be checked
  45.   *
  46.   * @return {boolean} exists?, true if exists, false if not exists
  47.   */
  48. check : function(set) {
  49. return ((App.Quick[set] && typeof(App.Quick[set]) === 'function') === true) ? true : false;
  50. },
  51.  
  52. /**
  53.   * function that adds new entries to the App.Quick set in realtime
  54.   * @params {string} set, which contains the label of the set item to be added
  55.   * @params {mixed objec} Value, the reference value to be connected with the set
  56.   * @params (boolean) forced, when set to true, it will override any existing one with the same label,
  57.   * this is also the default value (false, will not override)
  58.   *
  59.   * @return {boolean} execution state (true is oké, false is no good)
  60.   */
  61. set : function(set, Value, forced) {
  62.  
  63. if (typeof(set) === 'undefined' || typeof(Value) === 'undefined') {
  64. return false;
  65. }
  66.  
  67. var is_forced = (typeof(forced) === 'undefined') ? true : forced;
  68. if (typeof(App.Quick[set]) !== 'undefined' && is_forced === false) return false;
  69.  
  70. if (typeof(set) !== 'string') return false;
  71.  
  72. App.Quick.settings[set] = Value;
  73.  
  74. App.Quick[set] = function() {
  75. return App.Quick.settings[set];
  76. }
  77. return true;
  78.  
  79. },
  80.  
  81. /**
  82.   * hardcoded sample (please this sample assumes that App.Quick.StructureManager has
  83.   * been set somewhere in the software)
  84.   * THIS IS NOT VERY PRACTICAL, BUT FOR THE DEMO ITS OKÉ
  85.   */
  86. TreeNodeSelectedSemester : function() {
  87. return App.Quick.StructureManager()._selectedSemester;
  88. }
  89. }
  90.  
  91. }();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.