jquery - JSON is being globally modified in my JavaScript... why? -
below functions have use official jquery template plugin , insert json backend devs giving me in var
s toppages
, latestpages
reason when call insertorhidelist()
function , call normal renderlist()
function it's carrying on max
attribute reason. so, instead of getting 100
results (see 2nd line of renderlist()
im getting 5
see insertorhidelist()
calls @ bottom)
any ideas?
function renderlist(json,max){ if(!max){max=100} tempjson = json; tempjson.length = max; the_template = $.template(null, '<li><a href="{{if content_id}}article/${content_id}{{else}}${category_tree_id}{{/if}}" title="go ${title} page">${title}</a></li>'); return $.tmpl(the_template,tempjson); } function insertorhidelist(selector,json,max){ if(!max){max=5} if(typeof json !== 'undefined' && json.length >= 5){ $(selector).append(renderlist(json,max)); } else{ $(selector).parent().remove(); } } insertorhidelist('.most-popular ol',toppages,5); insertorhidelist('.recently-added ol',latestpages,5); console.log(renderlist(latestpages));
when this:
tempjson = json;
that global tempjson
array set same reference json
(latestpages
), changes you're making it, made original. new array work with, need make copy, this:
var tempjson = json.slice(0);
note var
addition, we're not creating global variable either (a separate issue here).
Comments
Post a Comment