HOME > > > >

on

object.on(event, callback, [context])Alias: bind

event
string
callback
function
[context]
?

サンプル

実行結果

実行削除

Javascript

  1. var debug1 = new Debug('#code1-dump');
  2.  
  3.  
  4. var Counter = function(){
  5.     this._count = 0;
  6. };
  7.  
  8. Counter.prototype = {
  9.     get: function(){
  10.         return this._count;
  11.     },
  12.  
  13.     up: function(){
  14.         this._count = this._count + 1;
  15.     },
  16.  
  17.     reset: function(){
  18.         this._count = 0;
  19.     }
  20. };
  21.  
  22. var counter  = new Counter();
  23. _.extend(counter, Backbone.Events);
  24.  
  25. counter.on('output', function(){
  26.     counter.up();
  27.     debug1.message('outputが ' + counter.get() + '回、triggerがされました。<br />');
  28. });
  29.  
  30. counter.on('reset', function(){
  31.     counter.reset();
  32.     debug1.clear();
  33. });
  34.  
  35. $('span#code1-execute').on('click', function(e){
  36.     counter.trigger('output');
  37. });
  38.  
  39. $('span#code1-console-clear').on('click', function(e){
  40.     counter.trigger('reset');
  41. });

HTML

  1. <span class="cq-btn" id="code1-execute">実行</span><span class="cq-btn" id="code1-console-clear">削除</span>
  2. <div id="code1-dump"></div>
参照サイト