Return to Snippet

Revision: 19236
at October 19, 2009 07:23 by rportela


Initial Code
//Some utilities for use in a typical widget that runs on W3C compliant Widget Runtimes
//I am using jQuery's support for cookies, so if you are not using it, please tweak the code at will to fit your needs
WidgetUtilities.prototype = {
	//Get the value associated with a configuration key
	getKey : function(key){
		if (typeof(widget) != "undefined"){ // A Widget Engine!
			return widget.preferenceForKey(key);
		}else{ // It's a browser!
			return $.cookie(key);
		}
	},
	//Set the value associated with a configuration key
	setKey : function(key, value){
		if (typeof(widget) != "undefined"){
			return widget.setPreferenceForKey(value, key);
		}else{
			return($.cookie(key, value));
		}
	},
	//Get the Pixels Per Inch on the device running the widget
    getWidth : function(){
		var DOM_body = document.getElementsByTagName('body')[0];
		
		var DOM_div = document.createElement('div');
		try{
			DOM_div.style = 'width: 1in; visibility:hidden;';
		}
		catch(e){
			
		}
		
		DOM_body.appendChild(DOM_div);
		var w = document.defaultView.getComputedStyle(DOM_div, null).getPropertyValue('width');
		DOM_body.removeChild(DOM_div);
		
		return parseInt(w);
	},
	//Run a specified function in a "safe" manner. If execution throws an exception, show all available information
	catchy : function(fn){
		return function(){
			try{
				fn.apply(fn, arguments);
			}catch(e){
				var err = [];
				for (var key in e){
					if (!key || typeof key=="function") continue;
					err.push(key + ": " + e[key]);
				}
				widget.showNotification(err.join("\n"));
			}
		};
	}
}

Initial URL

                                

Initial Description
Here are several functions I think are very helpfull while building mobile widgets (W3C), ready for your copy-paste pleasure =D

Initial Title
Widget utilities for W3C compliant Widget Runtimes

Initial Tags

                                

Initial Language
JavaScript