In our application we are caching some frequently used data. Recently we have developed a jquery plugin for our web app. Some of the plugin methods needs to access application cache.
I tried to access the cache, and result is undefined.
For example:
Application Class
Application(){
cachedObjects = {};
}
<body>
$(function(){
Application();
});
Plugin:
(function( $ ){
var methods = {
initialize : function( options ) { },
getDataFromCache : function() {
var data = Application.cachedObjects.someObject;// Here, data is undefined.
}
};
$.fn.menuPlugin = function( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.initialize.apply( this, arguments );
} else {
}
};
})( jQuery );
How can i access those application objects?
Any suggestions would be appreciative.
Thanks!
There are many problems here. First you define a function without the
functionkeyword. Second you declarecachedObjectswithout thevarkeyword, so it becomes global. I think what you mean isIn any case
cachedObjectswill only be a local variable insideApplication, not a property of theApplicationobject (which happens to be a function).You can declare an
Applicationobject in a much simpler way withIn any case, if you are developing a jQuery plugin you should not create a global variable
Application; you can use a variable local to your plugin likeor store the cached data in a closure.