jquery - call different functions based on a variable -


really, i'm looking @ pattern in code , realizing potentially simplified. so, that's ultimate goal. plugin i'm writing calls different functions based on existence of data attributes.

var = $.extend({},$.bcplugins.defaults,options); var b;  // variable each plugin's data attribute var crumbsinstances = doc.find('[data-bcp-crumbs]'),     copyrightinstances = doc.find('[data-bcp-copyright]'),     activenavinstances = doc.find('[data-bcp-activenav]');   // determine wich functions called if (crumbsinstances.length) {     crumbsinstances.each(function() {         b = {};         b = $$.extend({}, b, $(this).data(a.dataoptions));         crumbs(a,b);     }); } if (copyrightinstances.length)  {     copyrightinstances.each(function()  {         b = {};         b = $$.extend({}, b, $(this).data(a.dataoptions));         copyright(a,b);     }); } if (activenavinstances.length)  {     activenavinstances.each(function()  {         b = {};         b = $$.extend({}, b, $(this).data(a.dataoptions));         activenav(a,b);     }); } 

this plugin have growing number of functions on time. can reduce down 1 if statement through variables?

maybe this:

function processinstance($obj, callfunc){   if ($obj.length)  {     $obj.each(function()  {         b = {};         b = $$.extend({}, b, $(this).data(a.dataoptions));         callfunc(a,b);     });   } }  var crumbsinstances = doc.find('[data-bcp-crumbs]'),     copyrightinstances = doc.find('[data-bcp-copyright]'),     activenavinstances = doc.find('[data-bcp-activenav]');  processinstance(crumbsinstances, crumbs); processinstance(copyrightinstances, copyright); processinstance(activenavinstances, activenav); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -