Skip to main content

Posts

Showing posts from February, 2014

JS: Phonegap/Cordova integration with JqueryMobile

Here is the standard way of loading and detecting Cordova & JqueryMobile framework. /* @author Abhishek Kumar @email akbittu@gmail.com */ var app = { deviceReadyDeferred, jqmReadyDeferred, initialize: function() { this.deviceReadyDeferred = $.Deferred(); this.jqmReadyDeferred = $.Deferred(); this.bindEvents(); }, bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); $(document).one("mobileinit", this.onJQMReady); $.when(this.deviceReadyDeferred, this.jqmReadyDeferred).then(this.onCompleteReady); }, onDeviceReady: function() { app.deviceReadyDeferred.resolve(); }, onJQMReady: function() { app.jqmReadyDeferred.resolve(); }, onCompleteReady: function() { main(); } };

JS: Singleton based Cache

Here is the cache class based on singleton design pattern in javascript. There are 2 alternatives present in here. /* @file CCache.js @author Abhishek Kumar @email akbittu@gmail.com */ // Singleton Class - Alternate#1 var CCache = (function () { var instance; function init() { var cache = {}; return { getCacheItem: function (itemId) { return cache[itemId]; }, setCacheItem: function (itemId, itemVal) { cache[itemId] = itemVal; } }; }; return { getInstance: function () { if (!instance) { instance = init(); } return instance; } }; })(); // Usage var oCache = CCache.getInstance(); oCache.setCacheItem('dataHolder', 'a new item value'); config.log(oCache.getCacheItem('dataHolder')); // Singleton Class - Alternate#2 function CCache() { if (arguments.callee._sing