2015-06-10 30 views
1

我对流星相当陌生,对JS也相对陌生。停止流星http减缓页面请求

我使用的代码是:

服务器/ methods.es6

var cheerio = Meteor.npmRequire('cheerio'); 

/*****************************************************************************/ 
/* Server Only Methods */ 
/*****************************************************************************/ 
Meteor.methods({ 
    /* 
    * Example: 
    * 
    * '/app/items/insert': function (item) { 
    * } 
    */ 
    player: function() { 
    const url = 'http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0-B199-2E2A11D1CC13/2014/fut/items/web/165434.json'; 

    const response = Meteor.http.get(url); 

    return response; 
    } 
}); 

客户端/模板/汽车/ cars_list.es6

Meteor.call('player', function (err, res) { 
    if (err) console.log(err); 

    console.log(JSON.parse(res.content)); 

    Session.set('player', JSON.parse(res.content)); 
}); 

/*****************************************************************************/ 
/* CarsList: Event Handlers */ 
/*****************************************************************************/ 
Template.CarsList.events({ 
}); 

/*****************************************************************************/ 
/* CarsList: Helpers */ 
/*****************************************************************************/ 
Template.CarsList.helpers({ 
    cars: function() { 
    return Cars.find(); 
    }, 
    player: function() { 
    return Session.get('player'); 
    } 
}); 

客户端/templates/cars/cars_list.html

<template name="CarsList"> 
    <h1>Cars List</h1> 

    {{ player.Item.FirstName }} 

    <table class="table table-hover"> 
    <thead> 
     <tr> 
     <th>Brand</th> 
     <th>Model</th> 
     <th>Fuel Type</th> 
     <th>Body Style</th> 
     <th>Top Speed</th> 
     <th>Power</th> 
     <th>Edit</th> 
     </tr> 
    </thead> 

    <tbody> 
     {{# each cars }} 
     {{> car }} 
     {{/ each }} 
    </tbody> 
    </table> 
</template> 

它大部分只是测试代码来尝试和执行HTTP请求。

当我删除所有与player有关的代码时,一切都是即时的。当player代码在那里时,页面立即加载,但cars数据直到player的HTTP请求得到解决才显示。有没有办法把HTTP请求放在后台,而其他所有的东西都做它以前做的事情?

还有一种方法来循环HTTP请求,并显示数据,当我收到它回来?

回答

1

我会建议你把方法代码放在你的both文件夹中。这样,延迟补偿不会受到影响,您的用户界面将按照预期更新。

如果您需要隐藏客户端的部分代码,您可以将它放在clientserver文件夹中。我没有测试过这个,但我假设如果客户端调用的结果不同(即他把它和控制台搞混了),你将把它的数据回滚到服务器端。

这样,您可以在服务器尚未返回时模拟方法结果。但是,请记住,无论您使用哪种设置,最终都必须等待网站数据继续运行,无论它在客户端还是服务器上。 如果你这样称呼你的数据,我认为它不仅停止你的mongo数据,而且你的js函数执行(事件循环)。我可能是错的(新手也在这里)

也许你应该阅读约fiber and future, and the async calls。在我看来,这是一条路。

祝你好运!

+0

我打算做的是抓取一些数据的网站,然后它会吐出来的客户端,因为它得到它,但这样做似乎阻止我的Mongo数据拉直,直到所有请求都得到解决 –

+0

真棒谢谢这绝对是对我来说正确的一步。负载很快,所以将其标记为接受:) –