PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/media/js/libs/backbone-eventbroker.js

https://bitbucket.org/kandal/nav-default
JavaScript | 213 lines | 55 code | 8 blank | 150 comment | 15 complexity | e188b857d9511c9b8018c218cd599ea8 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * The Backbone.EventBroker adds a general purpose Event Broker implementation
  3. * to Backbone based on the Backbone Events API. The EventBroker can be used
  4. * directly to serve as a centralized event management mechanism for an entire
  5. * application. Additional, context specific, namespaced brokers can also be
  6. * created in order to provide unique brokers within a particular part of an
  7. * application.
  8. */
  9. Backbone.EventBroker = Backbone.EventBroker || ( function() {
  10. // Defines the cache which contains each namespaced EventBroker instance
  11. var _brokers = {};
  12. /*
  13. * Implements the registering and unregistering of event/callback mappings
  14. * for specific objects registered with an EventBroker.
  15. */
  16. var _registration = function( interests, context, broker, method ) {
  17. if ( !context && interests.interests ) {
  18. context = interests;
  19. interests = interests.interests;
  20. }
  21. for ( var event in interests ) {
  22. if (interests.hasOwnProperty(event)) {
  23. broker[ method ]( event, context[ interests[event] ], context );
  24. }
  25. }
  26. return broker;
  27. };
  28. /*
  29. * Defines an Event registry which allows for registering and unregistering
  30. * multiple events/callbacks. This API is similar to Backbone.Events.on in
  31. * that it maps events to callbacks as well as a context. The main difference
  32. * is that mutliple event / callback mappings can be created as one-to-one
  33. * mappings for a given context.
  34. */
  35. var EventRegistry = {
  36. /*
  37. * Provides a convenience method which is similar to Backbone.Events.on
  38. * in that this method binds events to callbacks. The main difference is
  39. * that this method allows for binding multiple events / callbacks which
  40. * have a single context.
  41. *
  42. * This method can be invoked with two arguments, the first being an object
  43. * of event types as keys, whose values are the callbacks to which the events
  44. * are bound (as described above), and the second argument is the context
  45. * object typically (this) which specifies the context against which the
  46. * events and callbacks are to be invoked.
  47. *
  48. * This method can also be invoked with a single object which defines an
  49. * 'interests' property which defines a hash containing event types as
  50. * keys, and callbacks as values.
  51. *
  52. * <pre>
  53. *
  54. * // Register event/callbacks based on a hash and associated context
  55. * var Users = Backbone.Collection.extend(
  56. * {
  57. * broker: Backbone.EventBroker,
  58. *
  59. * initialize: function()
  60. * {
  61. * this.broker.register({
  62. * 'user:select' : 'select',
  63. * 'user:deselect' : 'deselect',
  64. * 'user:edit' : 'edit',
  65. * 'user:update' : 'update',
  66. * 'user:remove' : 'remove'
  67. * }, this );
  68. * },
  69. * select: function() { ... },
  70. * deselect: function() { ... },
  71. * edit: function() { ... },
  72. * update: function() { ... },
  73. * remove: function() { ... }
  74. * });
  75. * </pre>
  76. *
  77. * <pre>
  78. *
  79. * // Register event/callbacks based on an object's "interest" property.
  80. * var User = Backbone.Model.extend(
  81. * {
  82. * broker: Backbone.EventBroker,
  83. *
  84. * interests : {
  85. * 'user:select' : 'select',
  86. * 'user:deselect' : 'deselect',
  87. * 'user:edit' : 'edit',
  88. * 'user:update' : 'update',
  89. * 'user:remove' : 'remove'
  90. * },
  91. * initialize: function() {
  92. * this.broker.register( this );
  93. * },
  94. * select: function() { ... },
  95. * deselect: function() { ... },
  96. * edit: function() { ... },
  97. * update: function() { ... },
  98. * remove: function() { ... }
  99. * });
  100. * </pre>
  101. *
  102. */
  103. register: function( interests, context ) {
  104. if ( interests || context ) {
  105. return _registration( interests, context, this, 'on' );
  106. }
  107. return this;
  108. },
  109. /*
  110. * Provides a convenience method which is similar to Backbone.Events.off
  111. * in that this method unbinds events from callbacks. The main difference
  112. * is this method allows for unbinding multiple events / callbacks.
  113. *
  114. * This method can also be invoked with a single object which provides an
  115. * 'interests' property which defines a hash containing event types as keys,
  116. * and the callbacks to which the events were previosuly bound as values.
  117. *
  118. * This method can also be invoked with two arguments, the first being an
  119. * object of event types as keys, whose values are the callbacks to which
  120. * the events are bound (as described above), and the second argument is
  121. * context object typically (this) which specifies the context to which the
  122. * events and callbacks were bound.
  123. *
  124. * <pre>
  125. *
  126. * var UserView = Backbone.View.extend(
  127. * {
  128. * interests: {
  129. * 'user:select' : 'select',
  130. * 'user:deselect' : 'deselect'
  131. * },
  132. * initialize: function() {
  133. * Backbone.EventBroker.register( this );
  134. * },
  135. * remove: function() {
  136. * Backbone.EventBroker.unregister( this );
  137. * },
  138. * select: function() { ... },
  139. * deselect: function() { ... }
  140. * });
  141. * </pre>
  142. *
  143. */
  144. unregister: function( interests, context ) {
  145. if ( interests || context ) {
  146. return _registration( interests, context, this, 'off' );
  147. }
  148. return this;
  149. }
  150. };
  151. // creates and returns the EventBroker ...
  152. return _.extend({
  153. /*
  154. * Defines the default EventBroker namespace - an empty string. Specific
  155. * EventBrokers created via EventBroker.get( namespace ) will be created
  156. * and have their namespace property assigned the value of the namespace
  157. * specified.
  158. *
  159. */
  160. namespace: '',
  161. /*
  162. * Retrieves the broker for the given namespace. If a broker has yet to
  163. * have been created, it will be created for the namespace and returned.
  164. * Subsequent retievals for the broker of the same namespace will return
  165. * a reference to the same broker; that is, only one unique broker will
  166. * be created per namespace.
  167. *
  168. */
  169. get: function( namespace ) {
  170. if ( _brokers[ namespace ] === undefined ) {
  171. _brokers[ namespace ] = _.extend( { 'namespace': namespace }, Backbone.Events, EventRegistry );
  172. }
  173. return _brokers[ namespace ];
  174. },
  175. /*
  176. * Determines if the specified broker has been created for the given
  177. * namespace.
  178. *
  179. */
  180. has: function( namespace ) {
  181. return _brokers[ namespace ] !== undefined;
  182. },
  183. /*
  184. * Destroys the broker for the given namespace, or multiple brokers for
  185. * a space delimited string of namespaces. To destroy all brokers that
  186. * have been created under any namespace, simply invoke this method with
  187. * no arguments.
  188. *
  189. */
  190. destroy: function( namespace ) {
  191. if ( !namespace ) {
  192. for ( namespace in _brokers ) {
  193. if (_brokers.hasOwnProperty(namespace)) {
  194. this.destroy( namespace );
  195. }
  196. }
  197. }
  198. else if ( _brokers[ namespace ] ) {
  199. _brokers[ namespace ].off();
  200. delete _brokers[ namespace ];
  201. }
  202. return this;
  203. }
  204. }, Backbone.Events, EventRegistry );
  205. }());