• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

In prototype.js, Event.observe method is very useful. But it is a bit inconvenient if you want to handle events only once such as an initialization. I have often encountered such situation, so I wrote a small utility function being as follows.

   1  Event.observeOnce = function(element, event, observer){
   2    var handler = function(){
   3      Event.stopObserving(element, event, handler);
   4      return observer.apply(null, $A(arguments));
   5    };
   6    Event.observe(element, event, handler);
   7  };

The usage is same to Event.observe.

   1  Event.observeOnce(document, 'dom:loaded', function(){
   2    /* some code goes here */
   3  });

It may be useful if you copy this into public/javascripts/application.js for Rails applications.

posted by Png takiuchi on Thu 28 Feb 2008 at 17:23

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry391
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ