APIs

Show:
  1. /**
  2. The ComBroker is lite weight event bus that can be used inside MV* frameworks and offer services for the application.
  3. Services provided include registration and query of data members, registration and query of
  4. instances (often registered instances are service providers themselves) and a central location
  5. for binding and triggering of events.
  6. @class ComBroker
  7. @constructor
  8. @return {Object} instantiated ComBroker
  9. @example
  10. <pre>
  11. Backbone.comBroker = new ComBroker.bus();
  12. Backbone.comBroker.setService('me',function(i_var){
  13. alert('I am a service ' + i_var)});
  14. var g = com.getService('me');
  15. g("hello again");
  16. $(com).bind('change',function(e){
  17. alert('pop ' +e);
  18. });
  19. $(Backbone.comBroker).triggerHandler('change');
  20. example: Backbone.comBroker.fire(loginManager.LOGINBUTTON, this, '#loginButton', "hellow world" );
  21. example: Backbone.comBroker.listen(loginManager.AUTHENITCATING,loginManager.LOGINBUTTON,function(e){});
  22. </pre>
  23. **/
  24. define(['jquery', 'backbone'], function ($, Backbone) {
  25.  
  26. Backbone.EVENTS = Backbone.EVENTS ? Backbone.EVENTS : {};
  27. Backbone.EVENTS.SERVICE_REGISTERED = 'SERVICE_REGISTERED';
  28.  
  29. var ComBroker = Backbone.Controller.extend({
  30.  
  31. /**
  32. Constructor
  33. @method initialize
  34. **/
  35. initialize: function () {
  36. this.m_services = [];
  37. Backbone.EVENTS.SERVICE_REGISTERED = 'SERVICE_REGISTERED'
  38. },
  39.  
  40. /**
  41. Register a data member that others can query.
  42. @method setValue
  43. @param {String} i_name
  44. @param {Object} i_value
  45. @param {Event} i_fireEvent
  46. @return none
  47. **/
  48. setValue: function (i_name, i_value, i_fireEvent) {
  49. this.m_services[i_name] = i_value;
  50. if (i_fireEvent)
  51. this.fire(i_name, this, null, {edata: i_value})
  52. },
  53.  
  54. /**
  55. Get a registered data member.
  56. @method getValue
  57. @param {String} i_name
  58. @return {Object} m_services member
  59. **/
  60. getValue: function (i_name) {
  61. if (this.m_services[i_name]) {
  62. return this.m_services[i_name]
  63. } else {
  64. return undefined;
  65. }
  66. },
  67.  
  68. getFramework: function(){
  69. var self = this;
  70. return window.BB;
  71. },
  72.  
  73. /**
  74. Register a service that others can query.
  75. @method setService
  76. @param {String} i_name
  77. @param {Object} i_service
  78. @return none
  79. **/
  80. setService: function (i_name, i_service) {
  81. this.m_services[i_name] = i_service;
  82. this.fire(Backbone.EVENTS['SERVICE_REGISTERED'], this, null, {name: i_name, service: i_service})
  83. },
  84.  
  85. /**
  86. Get a registered service.
  87. @method getService
  88. @param {String} i_name
  89. @return {Object} m_services member
  90. **/
  91. getService: function (i_name) {
  92. if (i_name == undefined) {
  93. //log('cant get set undefined service ' + i_name);
  94. return undefined;
  95. }
  96. if (this.m_services[i_name]) {
  97. return this.m_services[i_name]
  98. } else {
  99. return undefined;
  100. }
  101. },
  102.  
  103. /**
  104. Expose all services and data members.
  105. @method getAllServices
  106. @return {Object} m_services
  107. **/
  108. getAllServices: function () {
  109. return this.m_services;
  110. },
  111.  
  112. /**
  113. Clear all current registered services
  114. @method clearServices
  115. **/
  116. clearServices: function () {
  117. var self = this;
  118. // delete self.m_services;
  119. self.m_services = undefined;
  120. },
  121.  
  122. /**
  123. Trigger an event within the context of the CommBroker thus reducing DOM capture / bubble.
  124. @method fire
  125. @param {Event} i_event
  126. @param {Event} i_context
  127. @param {Event} i_caller
  128. @param {Event} i_data
  129. @return none
  130. **/
  131. fire: function (i_event, i_context, i_caller, i_data) {
  132. $(this).trigger(this.event(i_event, i_context, i_caller, i_data));
  133. },
  134.  
  135. /**
  136. Listen to events within the context of the CommBroker thus reducing DOM capture / bubble.
  137. Once the even is triggered func will get called back.
  138. @method listen
  139. @param {Event} events
  140. @param {Function} func
  141. @return none
  142. **/
  143. listen: function (events, func) {
  144. if (arguments.length > 2) {
  145. var totalArgs = Number([arguments.length - 1]);
  146. var events = $(arguments).splice(0, totalArgs);
  147. var func = arguments[totalArgs]
  148.  
  149. for (var i = 0; i < events.length; i++) {
  150. events[i] = "'" + events[i] + "'";
  151. }
  152. events = events.join(',');
  153. return $(this).bind(eval(events), func);
  154. } else {
  155. return $(this).bind(events, func);
  156. }
  157. },
  158.  
  159. /**
  160. Listen to events within the context of the CommBroker thus reducing DOM capture / bubble.
  161. However we only listen within the namespace of a unique context id so we can remove it
  162. later for a specific listener instance.
  163. @method listenWithNamespace
  164. @param {Event} events
  165. @param {Object} caller
  166. @param {Function} call back
  167. @return none
  168. **/
  169. listenWithNamespace: function (event, caller, func) {
  170. if (caller.eventNamespace == undefined)
  171. caller.eventNamespace = _.uniqueId();
  172. var namespacEvent = event + '.' + caller.eventNamespace;
  173. $(this).bind(namespacEvent, func);
  174. },
  175.  
  176. /**
  177. Stop listening to an event but only within the context of a specific listener instance.
  178. @method stopListenWithNamespace
  179. @param {String} event
  180. @param {Function} func
  181. @return none
  182. **/
  183. stopListenWithNamespace: function (event, caller) {
  184. var namespacEvent = event + '.' + caller.eventNamespace;
  185. $(this).unbind(namespacEvent);
  186. },
  187.  
  188. /**
  189. Listen only once to an event and unbind.
  190. Once the event is triggered func will get called back.
  191. @method listenOnce
  192. @param {Event} events
  193. @param {Function} func
  194. @return none
  195. **/
  196. listenOnce: function (events, func) {
  197. $(this).one(events, func);
  198. },
  199.  
  200. /**
  201. Stop listening to an event.
  202. @method stopListen
  203. @param {Event} events
  204. @param {Function} func
  205. @return none
  206. **/
  207. stopListen: function (events, func) {
  208. if (func == false) {
  209. $(this).unbind(events);
  210. } else {
  211. $(this).unbind(events, func);
  212. }
  213. },
  214.  
  215. /**
  216. The jQuery.Event constructor is exposed and can be used when calling trigger. The new operator is optional.
  217. @method event
  218. @param {Event} i_event
  219. @param {Object} i_context
  220. @param {Object} i_caller
  221. @param {Object} i_data
  222. @return none.
  223. **/
  224. event: function (i_event, i_context, i_caller, i_data) {
  225. return $.Event(i_event, {context: i_context, caller: i_caller, edata: i_data});
  226. }
  227. });
  228.  
  229. return ComBroker;
  230. });
  231.  
  232.  
  233.