javascript - 单 View 页面永久链接为空,带有 Iron Router

标签 javascript meteor iron-router

我在 Iron-Router 中设置了两条路由:“home”(所有帖子的分页列表)和“doc”(详细 View )。主页加载得很好,但只有在之前查看过主页的情况下才能加载详细 View 。否则它将呈现为空——并且不能用作永久链接。

这将始终加载: http://localhost:3000/

仅当之前查看过“home”时才会加载: http://localhost:3000/doc/tZFawq8cgf43hZBaJ

路线:

Router.map(function() {
    this.route('home', {
        path: '/'
    });
    this.route('doc', {
        path: '/doc/:_id',
        data: function() {
            return MyPix.findOne({_id: this.params._id});
        }
    });
});

文档模板:

<template name="doc">
    <h1>{{this.name}}</h1>
    <img src="{{ this.url store='OriginalRetinaPix' }}" width="{{ this.metadata.width }}" height="{{ this.metadata.height }}" />
</template>

发布/订阅:

Meteor.publish('MyPix', function(cursor) {
    Counts.publish(this, 'numberOfPosts', MyPix.find(), { noReady: true });
    return MyPix.find({}, {sort: {uploadedAt: -1}, limit: 4, skip: cursor});
});

if(Meteor.isClient) {
    Session.setDefault('docCursor', 0);
    console.log('docCursor: ' + Session.get('docCursor'));
    Meteor.autorun(function(){
        Meteor.subscribe('MyPix', Session.get('docCursor'));
    })
}

顺便说一句:GitHub 上的项目

最佳答案

在“doc”路线上,您应该使用 waitOn 以便在页面加载时准备好数据。在 Router.configure 中添加加载模板

我建议您升级到新的 iron:router 路由声明,并添加 meteorhacks:subs-manager 以更好地缓存订阅。

这是一个应该适用于您的情况的示例

var subs = new SubsManager();
Router.route('/doc/:_id', {
  name: 'doc',
  template: 'doc',
  waitOn: function() {
    return subs.subscribe('aPix', this.params._id);
  },
  data: function() {
    return {
      apix: MyPix.findOne({
        _id: this.params._id
      })
    };
  }
});

并在服务器端创建一个publications.js

Meteor.publish('aPix', function(id) {
  check(id, String);
  return MyPix.find(id);
});

关于javascript - 单 View 页面永久链接为空,带有 Iron Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28168471/

相关文章:

javascript - 当 jqGrid 中的 (“reloadGrid” 时可以触发 'loadonce: true' )吗?

javascript - 有没有办法防止网站文本被搜索到?

Javascript函数可以写入控制台但不返回值

javascript - Meteor 服务器在不使用时会关闭吗? Meteor.setInterval 似乎最终停止了

Meteor build TypeError : module. 导入不是函数

meteor - Meteor.js 模板 block 中的“或”条件

meteor 面包屑

javascript - Meteor:在发布中使用 Meteor._sleepForMs() 时 Iron router 不显示加载模板

javascript - 用户进入网站任何部分后隐藏 div

javascript - 是否有 Meteor Any route.ready 回调?为了更轻松地附加 jQuery 事件