Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Winton Welsh http://stu.dicio.us

« Newer Snippets
Older Snippets »
4 total  XML / RSS feed 

Attach Events

// takes this.events with format 'element:event': function(e) { } and attaches it

attachEvent: function(selector) {
  var sel = selector.split(':');
  var el =  this.el[sel[0]];
  var ev =  sel[1];
  if (!el)      return;
  if (!el.each) Event.observe(el, ev, this.events[selector]);
  else el.each( function(item, index) {
    Event.observe(item, ev, this.events[selector]);
  }.bind(this));
},
attachEvents: function() {
  for (property in this.events) this.attachEvent(property);
},
removeEvent: function(selector) {
  var sel = selector.split(':');
  var el = this.el[sel[0]];
  var ev = sel[1];
  if (!el) return;
  if (!el.each) Event.stopObserving(el, ev, this.events[selector]);
  else el.each(function(item, index) {
    Event.stopObserving(item, ev, this.events[selector]);
  }.bind(this));
},
removeEvents: function() {
  for (x in this.events) this.removeEvent(x);
}

Attach Elements

// takes this.el and copies it to this.elements, replacing CSS selector with DOM object(s)

attachElement: function(p) {
  this.el[p] = $$(this.elements[p]);
  if (this.el[p].length == 1) this.el[p] = this.el[p][0];
},
attachElements: function() {
  this.el = {};
  for (x in this.elements) this.attachElement(x);
},
clearElements: function() {
  for (x in this.el) this.el[x] = null;
}

Bind Functions

// these functions force a common 'this' on inner objects

bindTo: function(source, bindTo) {
  if (typeof source == 'function') return;
  for (x in source)
    if (typeof source[x] == 'function')
      source[x] = source[x].bind(bindTo);
},
bindAll: function() {
  this.bindTo(this.events,   this);
  this.bindTo(this.ajax,     this);
  this.bindTo(this.complete, this);
}

Custom Ajax.Request Function

// this is a custom function that simplifies creating an ajax object

function(url, complete, form, params) {
  params = params ? $H(params).toQueryString() : '';
  if (!complete) complete = function(r){};
  if (form)      params   = Form.serialize(form) + '&' + params;
  new Ajax.Request(url, 
    { asynchronous:true,
      evalScripts:true, 
      onComplete: function(r) { complete(r.responseText); },
      parameters:params
    });
}
« Newer Snippets
Older Snippets »
4 total  XML / RSS feed