| | 259 | /** |
| | 260 | * Trigger the bound callbacks of a specific topic, or trigger the anonymous callbacks |
| | 261 | * |
| | 262 | * @param {string} the topic id |
| | 263 | * @param {to} the optional new value |
| | 264 | * @param {from} the optional previous value |
| | 265 | */ |
| | 266 | trigger: function( id, to, from ) { |
| | 267 | to = to || this(); |
| | 268 | from = from || this(); |
| | 269 | if ( this.topics && this.topics[ id ] ) { |
| | 270 | this.topics[ id ].fireWith( this, [ to, from ] ); |
| | 271 | } else { |
| | 272 | this.callbacks.fireWith( this, [ to, from ] ); |
| | 273 | } |
| | 274 | return this; |
| | 275 | }, |
| | 276 | |
| 250 | | * Bind a function to be invoked whenever the value changes. |
| 251 | | * |
| 252 | | * @param {...Function} A function, or multiple functions, to add to the callback stack. |
| 253 | | */ |
| 254 | | bind: function() { |
| 255 | | this.callbacks.add.apply( this.callbacks, arguments ); |
| 256 | | return this; |
| 257 | | }, |
| | 278 | * Bind a function to be invoked whenever the value changes. |
| | 279 | * @param {string} the optional id of the callbacks topic |
| | 280 | * @param {...Function} A function, or multiple functions, to add to the callback stack. |
| | 281 | */ |
| | 282 | bind: function( id ) { |
| | 283 | if ( _.isFunction( id ) ) { |
| | 284 | this.callbacks.add.apply( this.callbacks, arguments ); |
| | 285 | } else { |
| | 286 | this.topics = this.topics || {}; |
| | 287 | this.topics[ id ] = this.topics[ id ] || $.Callbacks(); |
| | 288 | this.topics[ id ].add.apply( this.topics[ id ], slice.call( arguments, 1 ) ); |
| | 289 | } |
| | 290 | return this; |
| | 291 | }, |
| 260 | | * Unbind a previously bound function. |
| 261 | | * |
| 262 | | * @param {...Function} A function, or multiple functions, to remove from the callback stack. |
| 263 | | */ |
| 264 | | unbind: function() { |
| 265 | | this.callbacks.remove.apply( this.callbacks, arguments ); |
| 266 | | return this; |
| 267 | | }, |
| | 294 | * Unbind a previously bound function. |
| | 295 | * |
| | 296 | * @param {string} the optional id of the callbacks topic |
| | 297 | * @param {...Function} A function, or multiple functions, to remove from the callback stack. |
| | 298 | */ |
| | 299 | unbind: function( id ) { |
| | 300 | if ( _.isFunction( id ) ) { |
| | 301 | this.callbacks.remove.apply( this.callbacks, arguments ); |
| | 302 | } else if ( this.topics && this.topics[ id ] ) { |
| | 303 | this.topics[ id ].remove.apply( this.topics[ id ], slice.call( arguments, 1 ) ); |
| | 304 | } |
| | 305 | return this; |
| | 306 | }, |
| | 307 | |
| | 308 | /** |
| | 309 | * Unbind all previously bound function of a callbacks topic if specified |
| | 310 | * Or unbind all functions if no topic is specified |
| | 311 | * |
| | 312 | * @param {string} optional topic id |
| | 313 | */ |
| | 314 | unbindAll: function( id ) { |
| | 315 | if ( this.topics && this.topics[ id ] ) { |
| | 316 | this.topics[ id ] = $.Callbacks(); |
| | 317 | } else if ( this.topics ) { |
| | 318 | this.topics = {}; |
| | 319 | } else { |
| | 320 | this.callbacks = $.Callbacks(); |
| | 321 | } |
| | 322 | return this; |
| | 323 | }, |