Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Many beginning jQuery people forget to save the results of a selection and
- just call $('foo') over and over. The only time you would need to do that is if the
- DOM changes. Otherwise you can just save the results somewhere when the
- selection is first made. Much more performant.
- Here's a mini jQuery plugin that just caches the results of a selector after
- the first time it's invoked.
- */
- (function($){
- $.stale = function(selector){
- return $.lazy.lookup[selector] || ($.lazy.lookup[selector] = $(selector));
- };
- $.fresh = function(selector){
- return ($.lazy.lookup[selector] = $(selector));
- }
- $.stale.lookup = {};
- })(jQuery);
- //Usage
- $.stale('#button').click(function(){
- $.stale('#button').val();
- });
- //if for some reason you need to refresh the lookup:
- $.fresh('#button');
Add Comment
Please, Sign In to add comment