Sails.js-从EJS文件获取异步数据 [英] Sails.js - Get async data from an EJS file

查看:138
本文介绍了Sails.js-从EJS文件获取异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在从EJS文件中获取异步数据的好方法?

Sails.js仅具有从数据库获取数据的异步方法。

Are there a good way to get async data from an EJS file?
Sails.js only have async methods to get data from a database.

我有一个 页面 及其相关内容 。在某些情况下,我想从另一个 Page 中获取特定的 Value (例如:在相关的页面模块中,导航模块...)。而且我需要在服务器端进行操作,以保持前端部分 SEO友好

I have a collection of Pages with their associated content Values. In some cases I want to get a specific Value from another Page (e.g.: in a related page module, a navigation module...). And I need to do that server side, to keep the front-end part SEO friendly.

更简单的方法应该是检索控制器内的所有 Pages Values ,然后公开一个函数到我的EJS以获得我需要的特定数据。但是我真的不想每次都从集合中检索所有数据。

The easier way should be to retrieve all the Pages and Values inside the controller, and then expose a function to my EJS to get the specific data I need. But I really don't want to retrieve each time all the data from a collection.

我正在使用CMS,因此可以避免创建新的控制器/视图对于每个我将要遇到的特定情况。

I'm working on a CMS so I would avoid creating a new controller/view for each specific cases that I will have.

编辑:

似乎没有好的方法那,所以我重新设计了控制器,使其更加智能。


It seems there is no good way to do that, so I reworked my controller to make it a bit more intelligent. It solve my problem for now.

推荐答案

假设我正确地理解了您...。

Assuming I understand you correctly....

数据通过将对象传递到视图函子中而发送到视图

data is sent to the view by passing an object into the view funciton

return res.view({data: "important data"});

我猜您想为中间渲染的某些特定数据调用方法。因为在 res.view()调用中准备好所有数据太慢了。据我所知这是不可能的。

I'm guessing that you want to call a method for some specific data mid-render. Because having all the data ready on the res.view() call is too slow. As far as I know this is not possible. That would almost certainly be "blocking" code.

我可以建议的最佳选择是在页面加载后使用客户端上的ajax捕获特定数据。使用风帆的最佳功能之一:)

Best option I could recommend is to grab specific data using ajax on the client after page load. One of the best features of using sails :)

这篇关于Sails.js-从EJS文件获取异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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