Created
October 11, 2010 11:17
-
-
Save nilcolor/620379 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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