流星/铁路由器:如何使用settings.json中的数据定义路线 [英] Meteor/Iron-Router: how to define routes using data from settings.json

查看:69
本文介绍了流星/铁路由器:如何使用settings.json中的数据定义路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于路由所应用的URL,我在settings.json中定义了一部分,例如

For the URL to which a route applies I have a part defined in settings.json, like this

baseUrl: '/private'

我的设置已发布,并且可以通过设置"集合(在客户端上)进行访问.所以我尝试了以下方法:

My settings are published and accessible through the collections 'Settings' (on the client). So I tried the following:

Meteor.subscribe('settings');

Deps.autorun(function () {
    var settings = Settings.findOne():

   if (settings) {
        Router.map(function () {
            this.route('project', {
                path: settings.baseUrl + '/:projectId,
               controller: 'ProjectController'
            });
        });
   } 
});

问题在于初始化期间尚无法在客户端上获得数据,因此我必须等到数据出现为止.到目前为止,这种方法还行不通.但是在花费很多时间之前,我想知道是否有人以前做过这个,还是可以告诉我这是否是正确的方法?

The problem is that during initialisation the data is not yet on the client available, so I have to wait until the data is present. So far this approach doesn't work (yet). But before spending many hours I was wondering if someone has done this before or can tell me if this is the right approach ?

推荐答案

更新后的答案:

我在存储库中发布了解决方案: https://github.com/parhelium/meteor-so-inject-data-to-html .通过打开url进行测试:localhost:3000/test

Updated answer:

I published solution in repository : https://github.com/parhelium/meteor-so-inject-data-to-html . Test it by opening url : localhost:3000/test

在这种情况下,FastRender软件包是无用的,因为它 head 标记->第63行的末尾插入集合数据.

In this case FastRender package is useless as it injects collection data in the end of head tag -> line 63.

Inject-Initial软件包将数据注入到 head 标签->第106行的开头.

Inject-Initial package injects data in the beginning of head tag -> line 106.

需要的包裹:

  mrt add iron-router
  mrt add inject-initial

源代码:

Settings = new Meteor.Collection("settings");
if (Meteor.isClient) {

    var settings = Injected.obj('settings');
    console.log(settings);
    Router.map(function () {
        this.route('postShow', {
            path: '/'+settings.path,

            action: function () {
                console.log("dynamic route !");
            }
        });
    });
}

if (Meteor.isServer){
    if(Settings.find().count() == 0){
        Settings.insert({path:"test",data:"null"});
    }

    Inject.obj('settings', Settings.findOne());
}

在页面底部阅读有关安全性的信息: https://github.com/gadicc /meteor-inject-initial/

Read about security in the bottom of the page : https://github.com/gadicc/meteor-inject-initial/

以下解决方案在这种特定情况下不起作用,因为FastRender在head标签的末尾插入数据.因此,在插入数据之前先对路径进行初始化.

Below solution won't work in this specific case as FastRender injects data in the end of head tag. Because of that Routes are being initialized before injected data is present.

当来自Settings集合的数据与html一起发送时,它将起作用. 您可以使用软件包 FastRender 来做到这一点.

It will work when data from Settings collection will be sent together with html. You can do that using package FastRender.

创建文件server/router.js:

 FastRender.onAllRoutes(function(path) {
     // don't subscribe if client is downloading resources
     if(/(css|js|html|map)/.test(path)) {
         return;
     }
     this.subscribe('settings');
 }); 

还创建publish函数:

Meteor.publish('settings', function () {
     return Settings.find({});
});

上面的代码意味着,如果用户打开应用程序的任何url,则客户端将订阅设置"发布,并且数据将在服务器上注入html并立即可供客户端使用.

The above code means that if user open any url of your app then client will subscribe to "settings" publication and data will be injected on the server into html and available for client immediately.

我使用这种方法能够将许多不同的域连接到流星应用程序,并因此发送了正确的数据.

I use this approach to be able to connect many different domains to meteor app and accordingly sent proper data.

这篇关于流星/铁路由器:如何使用settings.json中的数据定义路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆