我在我的編輯器中有這麼大的代碼塊,看起來好像可以做得更好。有條件的字符串 - 更好的方法?
本質上,根據'divert_poss
'的值,我需要渲染一個稍微不同的字符串。 'divert_poss
'是1或0,它的值影響如何在'divert_enabled
'和'warning_enabled
'
附近繪製表格的方式是什麼?下面的代碼確實有效,但是必須有更好的方法?
if (_store.divert_poss == 1)
{
// show divert info for those where divert_poss=1
return [
'<div class="medium-4', clearClass, ' column">',
'<div class="store-locator__infobox" id="store', index, '">',
'<div class="infobox__row infobox__row--marker">',
'<div class="infobox__marker', (letter.length > 1) ? ' infobox__marker--small' : '', '">',letter,'</div>',
'</div>','<div class="infobox__body">',
'<div class="infobox__row infobox__title store-image" style="display:none;">',_store.storeimage,'</div>',
'<div id="storeid" class="infobox__row infobox__title store-storeid" style="display:none;">',_store.storeid,'</div>',
'<div class="infobox__row infobox__title store-divert_poss" style="display:none;">',_store.divert_poss,'</div>',
'<div class="infobox__row infobox__title store-location"><center>',_store.location,'</div>',
'<div class="infobox__row store-address" style="display:none;">',_store.address, '</div>',
'<div class="infobox__row store-website" style="display:none;"><a target="new" href="http://',_store.website.replace(/(http:\/\/)\1/, '$1'),'">',_store.website, '</a></div>',
'<div class="infobox__row store-email" style="display:none;"><a href="mailto:',_store.email,'">',_store.email, '</a></div>',
'<div class="infobox__row store-tel" style="display:none;">Tel: ',_store.telephone, '</div>',
'<div class="infobox__row store-cat_id" style="text-align: center">',_store.cat_id, '</div>',
'<div class="infobox__row store-description" style="display:none;">',_store.description, '</div>',
'<div class="infobox__row store-specialism" style="display:none;">',_store.specialism, '</div>',
'<div class="infobox__row store-divert_enabled" style="display:none;">',
'<table width="100%"><tr><td align="center">Divert Status</td><td align="center">Warning Status</td></tr>',
'<tr><td align="center" width="50%">',_store.divert_enabled,'</td></div>',
'<div class="infobox__row store-warning_enabled" style="display:none;">',
'<td align="center" width="50%">',_store.warning_enabled, '</td></tr></table></div>',
'<div class="infobox__row store-embedvideo" style="display:none;">',_store.embedvideo, '</div>',
'<div class="infobox__row store-defaultmedia" style="display:none;">',_store.defaultmedia, '</div>',
'</div>',getDirections,
// '<div style="display:none;">',
// getStreetView,
// '</div>',
'</div>','</div>'].join('');
}else
{
// hide divert info for those where divert_poss=0
return [
'<div class="medium-4', clearClass, ' column">',
'<div class="store-locator__infobox" id="store', index, '">',
'<div class="infobox__row infobox__row--marker">',
'<div class="infobox__marker', (letter.length > 1) ? ' infobox__marker--small' : '', '">',letter,'</div>','</div>',
'<div class="infobox__body">','<div class="infobox__row infobox__title store-image" style="display:none;">',_store.storeimage,'</div>',
'<div id="storeid" class="infobox__row infobox__title store-storeid" style="display:none;">',_store.storeid,'</div>',
'<div class="infobox__row infobox__title store-divert_poss" style="display:none;">',_store.divert_poss,'</div>',
'<div class="infobox__row infobox__title store-location"><center>',_store.location,'</div>',
'<div class="infobox__row store-address" style="display:none;">',_store.address, '</div>',
'<div class="infobox__row store-website" style="display:none;"><a target="new" href="http://',_store.website.replace(/(http:\/\/)\1/, '$1'),'">',_store.website, '</a></div>',
'<div class="infobox__row store-email" style="display:none;"><a href="mailto:',_store.email,'">',_store.email, '</a></div>',
'<div class="infobox__row store-tel" style="display:none;">Tel: ',_store.telephone, '</div>',
'<div class="infobox__row store-cat_id" style="text-align: center">',_store.cat_id, '</div>',
'<div class="infobox__row store-description" style="display:none;">',_store.description, '</div>',
'<div class="infobox__row store-specialism" style="display:none;">',_store.specialism, '</div>',
'<div class="infobox__row store-divert_enabled" style="display:none;"></div>',
'<div class="infobox__row store-warning_enabled" style="display:none;"><table width="100%"><tr><td align="center" width="100%">Warning Info</td></tr><tr><td align="center">',_store.warning_enabled, '</td></tr></table></div>',
'<div class="infobox__row store-embedvideo" style="display:none;">',_store.embedvideo, '</div>',
'<div class="infobox__row store-defaultmedia" style="display:none;">',_store.defaultmedia, '</div>',
'</div>',getDirections,
// '<div style="display:none;">',
// getStreetView,
// '</div>',
'</div>','</div>'].join('');
}
}
難道你不能從這兩個字符串中提取交叉點並將其放入變量,並在if/else只是添加部分,這是不同的? – PatNowak
我會建議創建一個JSON數組並將其傳遞給一個名爲Mustache.js的有用JS插件 - 基本上,您將創建一個HTML模板並在init函數中傳遞數組。同時擺脫你的內聯樣式CSS,這很醜陋 –
字符串連接是計算最昂貴的事情之一,我個人認爲這是一個不好的做法。不過,我認爲這可能有助於[join vs concat](http://stackoverflow.com/questions/7299010/why-is-string-concatenation-faster-than-array-join#7299040) –