gpt4 book ai didi

javascript - Meteor JS - 按 ID 从集合中调用单个文档

转载 作者:行者123 更新时间:2023-11-30 05:44:16 24 4
gpt4 key购买 nike

我已经预先编写了 HTML,并希望从 DOM 中任何位置的集合中获取特定文档。

像这样:

<article>
<div class="content boxes-3">
<div class="box"></div>
<div class="box text">
<h2>Lorem of the Month</h2>
<p>
Lorem Upsum
</p>
</div>
<div class="box" data-product="0001"></div>
</div>
</article>
<article>
<div class="content">
<div class="box" data-product="0002"></div>
</div>
</article>
<article>
<div class="content boxes-2">
<div class="box" data-product="0002"></div>
<div class="box" data-product="0003"></div>
</div>
</article>

data-product 是我收藏中文档的 ID。

这可能吗?

注意:在与 Jim Mack 愉快交谈后,我编辑了这个问题归结为我的主要问题。

最佳答案

为了更好地理解原始问题,(并编辑以包含数据),

HTML:当你想引入时放入任何模板。

{{product id='0001' title='Description code' another='code' }}
{{product_for_catalog id='0002' title='Description code 2' }}

内部函数:

recordToTemplate = (options) ->
# allow for simple params as default
options.hash.title = options.hash.title or options.hash.fname
options.hash.template = options.hash.template or "product"
options.hash.placeholder = options.hash.placeholder or options.hash.title
options.hash.type = options.hash.type or 'text'
options.hash.data = Products.findOne({_id: id}); ## ADDED

new Handlebars.SafeString(Template[options.hash.template](options.hash))

助手:

Handlebars.registerHelper "product", (options) ->
# you can fix up here to set options defaults.
recordToTemplate.call this, options

Handlebars.registerHelper "product_for_catalog", (options) ->
# you can fix up here to set options defaults.
options.hash.template = 'catalog_product'
recordToTemplate.call this, options

模板:

<template name="product">
<div id="{{Id}}" class="product-action">
<div class="btn plus">
<i class="icon-remove"></i>
</div>
<div class="info">
{{#with data}}
<p>{{Desc}}</p>
<p>EUR {{Price}}</p>
{{/with}}
</div>
<div class="btn cart">
<i class="icon-shopping-cart"></i>
</div>
</div>
</template>

关于javascript - Meteor JS - 按 ID 从集合中调用单个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com