0

我正在使用 nodeJs .. 我有以下 javascript .. 从我的数据库中迭代一系列项目。我在做这个服务器端,这样数据是预先排序的,当使用把手时,我所要做的就是一个简单的嵌套每个语句..

这是从我使用geddy的时候开始的..

function transform ( arr ) {
   var result = [], temp = [];
   arr.forEach( function ( elem, i ) {
   if ( i > 0 && i % 3 === 0 ) {
   result.push( temp );
   temp = [];
   }
   temp.push( elem );
   });
   if ( temp.length > 0 ) {
   result.push( temp );
   }
   return result;
}

我已经把上面的js翻译成了coffeescript..

temp = []
result = []
for row, i in projects
  if i > 0 && i % 3 is 0
    result.push(temp)
    temp = []
  temp.push(projects)
if temp.length > 0
  result.push(temp)

话虽如此,我有以下咖啡脚本 ajax 请求:

$(document).ready ->
  $.ajax '/projects.json',
  dataType: 'json',
  success: (x,h,r) ->
    projects = x.data
    ## iterate through all of the projects

我试图在第三次迭代之后插入一些额外的 HTML 以完成以下操作:

在此处输入图像描述

构成上述屏幕截图的 html 是这样的,它的引导程序 2.3:

<div class="row-fluid">
    <ul class="thumbnails">    
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
    </ul>
</div>
<div class="row-fluid">
    <ul class="thumbnails">    
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
      <li class="span4 thumb-hover">
        <div class="thumbnail">
          <a href="/img/projects/" class="" target="_blank"><img src="/img/projects/thumbnails/admirecleaningservice_thumbnail.png" width="300px" height="200px"></a>
          <div class="mask">
            <h4>Admire Cleaning Services</h4>
            <p>Test 123</p>
          </div>
        </div>
      </li>
    </ul>
</div>

组织数组控制器端,传递给render方法然后让ejs只做两个嵌套循环,使用jquery和ajax请求注入或其他方法会更有益吗?

4

0 回答 0