patternjavascriptMinor
Seven days a week
Viewed 0 times
dayssevenweek
Problem
I use the jQuery Calendar with an underscore template and I wanted to put
There must be another (elegant) way of doing this.
tags around each week. This does the trick, but it's awful.
">
There must be another (elegant) way of doing this.
Solution
Interesting question,
you could definitely place the last
I would also place the first
So, something like this:
Personally, I would forego this templating for something like this:
you could definitely place the last
if outside of the _.each(), this would be faster ( fewer ifs ) and cleaner.I would also place the first
outside of the loop and then merge the 2 remaining if statements inside the loop.So, something like this:
">
Personally, I would forego this templating for something like this:
function fillTemplate( s ){
//Replace ~ with further provided arguments
for( var i = 1, a = s.split('~'), s = '' ; i ';
_.each( days, function( day, index ){
if( index && index % 7 )
html += '';
html += fillTemplate('~', day.classes, day.day );
});
html += '';Code Snippets
<li>
<% _.each(days, function(day, index) { %>
<% if( index && index % 7 ) { %></li><li><% } %>
<div class="<%= day.classes %>"><%= day.day %></div>
<% }); %>
</li>function fillTemplate( s ){
//Replace ~ with further provided arguments
for( var i = 1, a = s.split('~'), s = '' ; i < arguments.length ; i++ )
s = s + a.shift() + arguments[i];
return s + a.join("");
}
var html = '<li>';
_.each( days, function( day, index ){
if( index && index % 7 )
html += '</li><li>';
html += fillTemplate('<div class="~">~</div>', day.classes, day.day );
});
html += '</li>';Context
StackExchange Code Review Q#49627, answer score: 5
Revisions (0)
No revisions yet.