Source: mvc/controllers/helper.js

  1. /*
  2. * Copyright (C) 2012-2013 DFKI GmbH
  3. * Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4. * German Research Center for Artificial Intelligence
  5. * http://www.dfki.de
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. define(
  27. //this comment is needed by jsdoc2 [copy of comment for: function Helper(...]
  28. /**
  29. * The Helper Class is a kind of interface-class which gives access to the methods and data of a helper (which itself belongs to a controller)<br>
  30. *
  31. * @param {Controller} ctrl Controller instance / object
  32. * @param {String} name Name of the Helper
  33. * @param {Object} [ctx] OPTIONAL the context for the helper implementation (DEFAULT: global context, i.e. window)
  34. *
  35. * @name Helper
  36. * @class
  37. */
  38. function(
  39. ){
  40. //the next comment enables JSDoc2 to map all functions etc. to the correct class description
  41. /** @scope Helper.prototype */
  42. //set to @ignore in order to avoid doc-duplication in jsdoc3
  43. /**
  44. * @ignore
  45. *
  46. * The Helper Class is a kind of interface-class which gives access to the methods and data of a helper (which itself belongs to a controller)<br>
  47. *
  48. * @constructs Helper
  49. * @param {Controller} ctrl Controller instance / object
  50. * @param {String} name Name of the Helper
  51. * @param {Object} ctx the context for the helper implementation, i.e. where the constructor exists: ctx.<helper name>()
  52. */
  53. function Helper(ctrl, name, ctx){
  54. /**
  55. * The name of the helper.
  56. *
  57. * @type String
  58. * @public
  59. */
  60. this.name=name;
  61. /**
  62. * The controller to which this helper belongs.
  63. *
  64. * @type Controller
  65. * @public
  66. */
  67. this.controller = ctrl;
  68. /**
  69. * The definition of the helper object, i.e. its implementation,
  70. * containing all properties and functions of the controller.<br>
  71. *
  72. * A method of the helper can be called via:
  73. * <pre>
  74. * this.impl.method(parameter);
  75. * </pre>
  76. *
  77. * @type Object
  78. * @public
  79. */
  80. // this can only be invoked, if a function with the name "name" exists in the object/context ctx
  81. this.impl = new ctx[name](this);
  82. /**
  83. * @deprecated use {@link #impl} instead
  84. * @protected
  85. */
  86. this.script = this.impl;
  87. }
  88. /**
  89. * This function performs an action of a helper.<br>
  90. *
  91. * @function
  92. * @param {String} actionName Name of the method to be executed
  93. * @param {Object} data Data to pass to the method of the helper as argument
  94. * @returns {Object} The return value of the executed method
  95. * @public
  96. */
  97. Helper.prototype.perform = function(actionName, data){
  98. // if(logger.isv()) logger.v("should perform '" + actionName + "' of '" + this.name + "'" + ((typeof data !== 'undefined' && data !== null)? " with data: "+JSON.stringify(data): ""));//debug
  99. if(arguments.length > 2){
  100. return this.impl[actionName](this.controller, data, arguments[2]);
  101. }
  102. else {
  103. return this.impl[actionName](this.controller, data);
  104. }
  105. };
  106. return Helper;
  107. });//END: define(...