Skip to content

Instantly share code, notes, and snippets.

@nilcolor
Created October 11, 2010 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilcolor/620379 to your computer and use it in GitHub Desktop.
Save nilcolor/620379 to your computer and use it in GitHub Desktop.
(function($, window, document, undefined){
//handy log function
window.log = function(){
log.history = log.history || [];
log.history.push(arguments);
if(this.console)
console.log( Array.prototype.slice.call(arguments) );
};
$.extend($.expr[':'], {
lookup: function(a){return a.getAttribute('t2-ui-role') === 'lookup';}
});
var Lookup = {
options: {
version: '0',
icon: 'search'
},
currentlyActiveLookup: null,
_create: function(){
var self = this,
el = self.element;
self.options.icons.primary = self.icon;
//log('element IS'+(el.jquery?'':' NOT')+' jQuery object');
$.ui.button.prototype._create.apply(self);
self._setOption('icons', {
primary:'ui-icon-'+self.options.icon,
secondary:null
});
el.bind('click', function(e){
e.preventDefault();
e.stopPropagation();
$(e.target).trigger('lookup.t2', {lookup:this});
self._trigger('activated', null, {lookup:this});
return false;
});
},
_init: function(){ log('_init'); },
boom: function(){ log('nice, you boom me!'); }
};
$.widget('t2.lookup', $.ui.button, Lookup);//NS doesn't mean something yet
//====================== RUN CODE HERE ======================
$('#id_ref_button').button({icons:{primary: "ui-icon-search"}});
var l1 = $(':lookup').lookup({
activated: function(e, data){ //tightly coupled
log('activated callback fired');
log(' '+data, data.lookup);
}
});
// a-la Observer Pattern
$('body').bind('lookup.t2', function(e, data){ //loosely coupled v.1
log('lookup.t2 event catched');
log(' '+data, data ? data.lookup : 'nope');
});
$('body').bind('lookupactivated', function(e, data){ //loosely coupled v.2
log('lookupactivated event catched'); //why not activated.lookup?
log(' '+data, data.lookup);
});
l1.lookup('boom');
})(jQuery, window, window.document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment