【问题标题】:data context from router and recipes not showing来自路由器的数据上下文和配方未显示
【发布时间】:2015-10-20 04:56:32
【问题描述】:

我正在从路由器获取数据上下文,但食谱没有显示在我的食谱页面上。我想问一下我在这里做错了什么?

collections.js

Recipes = new Mongo.Collection('recipes');

router.js

Router.route('/recipes', function () {
    this.render('recipes',{
            name:'recipes'
        },
        {
            data: function(){
            return Recipes.find();
        }
    });
});

recipes.html

<template name="recipes">
    {{#each recipes}}
        <div class="container" style="margin-top:10px;">
            <div class="row form-group">
                <div class=" col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-image">
                            <img src="{{image}}" class="panel-image-preview" />
                        </div>
                        <div class="panel-body">
                            <h4>{{name}}</h4>
                            <p>{{description}}</p>
                        </div>
                        <div class="panel-footer text-center">
                            <a href="#"><span class="glyphicon glyphicon-open-file"></span></a>
                            <a href="#"><span class="glyphicon glyphicon-time" style="vertical-align:middle"></span><small> {{time}}</small></a>
                            <a href="#"><span class="glyphicon glyphicon-heart " style="vertical-align:middle"></span><small>15 </small></a>
                            <a href="#"><span class="glyphicon glyphicon-share"></span></a>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    {{/each}}
</template>

【问题讨论】:

  • 您在哪里订阅收藏?我们能看到那个代码吗?
  • 这里是github链接github.com/soni1/foody@gdataDan
  • 我认为你需要阅读http://docs.meteor.com/#/full/meteor_publish。据我所知,您没有发布/订阅集合中的数据,而且您的集合只是空的。
  • 我还没有删除默认的自动发布和不安全的包,所以暂时不需要发布数据。
  • 在您的食谱页面内部,Recipes.find().count() 是否返回大于 0 的数字?您的示例数据或插入数据的结构也是这样的:{recipes:{name:x,image:y},{name:x,image:y}} 等?

标签: meteor iron-router meteor-blaze


【解决方案1】:

你的路线搞砸了。您似乎以无法解析的方式混合了路线选项和渲染选项。此外,您的数据上下文没有配方作为字段。

也许以下就是你想要的?

Router.route('/recipes', function () {
    this.render('recipes', {
        data: function() {
            return { recipes: Recipes.find() };
        }
    });
}, {
   name:'recipes',
});

我个人更喜欢将数据添加到路由而不是渲染调用,并尽可能使用路由选项:

Router.route('/recipes', {
   name: 'recipes',
   template: 'recipes',
   data: function() {
       return {recipes: Recipes.find()};
   }
});

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2017-12-21
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多