将动态内容集成到 Sails.JS 应用程序中的 layout.ejs 文件的正确方法是什么? [英] What is the proper way to integrate dynamic content into the layout.ejs file in a Sails.JS application?

查看:39
本文介绍了将动态内容集成到 Sails.JS 应用程序中的 layout.ejs 文件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我用 Sails.js 编写了一个博客应用.

Say I wrote a blog app in Sails.js.

在此应用程序的每个页面上,都有一个名为Recent Posts"的侧边栏小部件,其中列出了 5 个最新帖子的标题,单击它们会将您带到相关帖子.

On every page in this app, there is a sidebar widget called "Recent Posts", where it lists the titles of the 5 most recent posts and clicking on them takes you to the post in question.

因为这个侧边栏小部件存在于每个页面,它应该在 layout.ejs 中.但是,这里我们有一个冲突 - 动态内容只能在控制器操作中从数据库中提取以呈现特定视图.

Because this sidebar widget is present on every page, it should be in layout.ejs. But, here we have a conflict - dynamic content is only supposed to be pulled from the database in the controller action for rendering a specific view.

动态内容不是针对特定视图,而是针对整个站点(通过 layout.ejs).

This dynamic content isn't for a specific view, it's for the whole site (via layout.ejs).

根据我理解的惯例,我必须在呈现视图的每个控制器操作中获取侧边栏小部件的动态内容数据(否则我会在尝试时收到未定义的错误在我的 layout.ejs 文件中调用该本地文件).

By the conventions that I understand, I'd have to get that dynamic content data for the sidebar widget in every controller action that renders a view (otherwise I would get an undefined error when I attempt to call that local in my layout.ejs file).

我尝试过/考虑过的事情:

Things I've tried / considered:

  • 呈现视图的每个控制器操作中加载该动态内容(这个解决方案非常糟糕)并在 layout.ejs 中调用该动态内容,就好像它是特定的本地内容一样看法.这工作正常,但不利于 D.R.Y.坦率地说,在每个控制器操作中都必须对数据库运行相同的查询是一件很痛苦的事情.

  • Load that dynamic content in every controller action that renders a view (this solution is very bad) and calling that dynamic content in layout.ejs as if it were a local for the specific view. This works fine, but goes against D.R.Y. principles and quite frankly is a pain in the ass to have to run the same query to the database in every controller action.

根据另一个类似的 stackoverflow 问题,创建一个新配置 (EG config/globals.js),将我的数据库中的动态内容作为变量加载到该配置文件中,然后在我的 layout.ejs 文件中调用 sails.config.globals.[variable_name] .这也有效,因为显然配置变量在应用程序中随处可用——但这是一个我不喜欢的黑客解决方案(我正在加载的内容只是最近 5 个帖子的标题和 slug,而不是一个全局配置选项",正如解决方案所暗示的那样).

As per another similar stackoverflow question, create a new config (E.G. config/globals.js), load my dynamic content from my database into that config file as a variable, and then calling sails.config.globals.[variable_name] in my layout.ejs file. This also worked, since apparently config variables are available everywhere in the application -- but it 's a hacky solution that I'm not a fan of (the content I'm loading is simply the titles and slugs of 5 recent posts, not a "global config option", as the solution implies).

运行查询以直接在某些 <% %> 标记之间获取 .EJS 文件中的动态内容.我不确定这是否行得通,但即使行得通,也违背了关注点分离 MVC 原则,我想尽可能避免这样做(如果可行的话).

Run the query to get the dynamic content inside the .EJS file directly between some <% %> tags. I'm not sure if this would work, but even if it did, it goes against the separation of concerns MVC principle and I'd like to avoid doing this if at all possible (if it even works).

根据冗长的 IRC 讨论@http://webchat.freenode.net/?channels=sailsjs,建议创建一个策略并将该策略映射到我的所有控制器.在该策略中,在数据库中查询 5 个最近的帖子,并将它们设置为 req.recentposts.这个解决方案的问题是,虽然最近的帖子数据将传递给每个控制器,但我仍然必须将 req.recentposts 数据传递给我的视图——这样我仍然需要修改每个 res.view({}) 在每个动作中.我不必在每个动作中都进行数据库查询,这很好,但我仍然必须为呈现视图的每个动作添加一行代码……这不是 D.R.Y.我正在寻找更好的解决方案.

As per a lengthy IRC discussion @ http://webchat.freenode.net/?channels=sailsjs, it was suggested to create a policy and map that policy to all my controllers. In that policy, query the database for the 5 most recent posts, and set them to the req.recentposts. The problem with this solution is that, while the recent posts data will be passed to every controller, I still have to pass that req.recentposts data to my view -- making it so I still have to modify every single res.view({}) in every action. I don't have to have the database query in every action, which is good, but I still have to add a line of code to every action that renders a view... this isn't D.R.Y. and I'm looking for a better solution.

那么,什么是正确的解决方案,而无需在每个控制器操作中加载动态内容(一种遵循 DRY 的解决方案)是我正在寻找的),以获得一些可用于我的 layout.ejs 文件的动态内容?

So, what is the proper solution, without needing to load that dynamic content in every controller action (a solution that adheres to D.R.Y. is what I'm lookng for), to get some dynamic content available to my layout.ejs file?

推荐答案

在文件夹/config 你应该创建一个文件 express.js 并添加类似的内容:

In folder /config you should create a file express.js and add something like that:

module.exports.express = {
    customMiddleware: function(app){
        app.use(function(req, res, next){
            // or whatever query you need
            Posts.find().limit(5).exec(function(err, posts){
                res.locals.recentPosts = posts;
                // remember about next()
                next();
            });
        });
    }
}

然后在你的视图中做一些简单的循环:

Then just make some simple loop in your view:

<% for(var i=0; i<recentPosts.length; i++) { %>
    <% recentPosts[i].title %>
<% } %>

以下是指向文档中适当位置的一些链接:https://github.com/balderdashy/sails-docs/blob/0.9/参考/Configuration.md#expresshttps://github.com/balderdashy/sails-docs/blob/0.9/参考/Response.md#reslocals

Here are some links to proper places in documentation: https://github.com/balderdashy/sails-docs/blob/0.9/reference/Configuration.md#express and https://github.com/balderdashy/sails-docs/blob/0.9/reference/Response.md#reslocals

这篇关于将动态内容集成到 Sails.JS 应用程序中的 layout.ejs 文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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