簡體   English   中英

Meteor.js-遍歷對象數組

[英]Meteor.js - Loop through array of objects

我有一個小的列表構建應用程序,幾乎是一個待辦事項列表。

我有一個名為“列表”的收藏

Lists = new Meteor.Collection('lists')

我有一個表單可以提交給此收藏集,並且效果很好。

我的模板在這里:

<template name="list">
  {{#with list}}
    <ul>
    {{#each links}}
    <li>PLEASE WORK! >>>> {{title}}</li>
    {{/each}}
    </ul>
  {{/with}}
</template>

我的助手在這里:

  Template.list.helpers({
    list: function () {
      currentListId = Session.get('currentListId')
      return Lists.find({
        _id : currentListId
      });
    },
    title: function () {
      return this.title
    }
  })

我知道我已訂閱數據庫,因為當我這樣做時:

Lists.find({_id : currentListId}).fetch()

它返回一個看起來像

_id: "mrkpjGW2"
createdAt: 1447401698770
items: Array[3]
__proto__: Object

和內部項目

items: Array[3]
    0: Object
        createdAt: 1447402263732
        owner: "3oyKZKhdPZyDkWnZm"
        title: "google.com"
        __proto__: Objec

所以我想遍歷項目,並獲得標題。

我沒有看到links助手。 您遍歷鏈接,但是定義了嗎? 此外,示例中的模板名稱也有所不同-HTML代碼中的list和JS中的linkList

您有{{#each links}},但是什么是鏈接? 我沒有在任何地方定義它。

你什么時候回來...

list: function () {
      currentListId = Session.get('currentListId')
      return Lists.find({
        _id : currentListId
      });
    }

您可以直接對其進行遍歷,因此可以直接使用頂部的{{#eachlist}}而不是頂部的{{#with list}}。

<template name="list">
    <ul>
    {{#each list}}
    <li>PLEASE WORK! >>>> {{title}}</li>
    {{/each}}
    </ul>
</template>

您已經使用了輔助程序來創建列表,這足夠了,也無需為標題創建輔助程序。 您可以迭代項目並直接從大火中獲取標題。
據我了解,您想遍歷items數組,並從每個項目中獲取標題。 因此,在javascript中,

Template.list.helpers({
    list: function () {
        currentListId = Session.get('currentListId')
        return Lists.find({
        _id : currentListId
      }).fetch();
    }
  })

在您的html中,

<template name="list">
    <ul>
        {{#each list}}
            <li>PLEASE WORK! >>>> {{title}}</li>
        {{/each}}
    </ul>
</template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM