简体   繁体   English

在Rails 3.1中动态创建JavaScript文件

[英]Dynamically creating javascript file in Rails 3.1

I am attempting to create a plug and play shopping cart in Rails 3.1 that allows users to add a shopping cart to their site by just adding a link to a javascript file. 我正在尝试在Rails 3.1中创建一个即插即用购物车,使用户可以通过仅向javascript文件添加链接来将购物车添加到其站点。 The items for sale are input on my end and stored in this js file and rendered with jquery templates. 待售商品在我端输入,并存储在此js文件中,并使用jquery模板呈现。 I currently have an action that renders the corresponding js, but I was wondering if there was a way to create a new minified js file for each site and link to this file in each site instead of the show action that renders the js. 我目前有一个呈现相应js的动作,但是我想知道是否有一种方法可以为每个站点创建一个新的缩小的js文件并链接到每个站点中的该文件,而不是呈现js的show动作。

For example, for store#1, I would like to create and save a js file called store1.js and serve that file instead of calling the show.js action that creates the javascipt array for the jquery templates every time. 例如,对于store#1,我想创建并保存一个名为store1.js的js文件并提供该文件,而不是每次调用都会为jquery模板创建javascipt数组的show.js操作。

You could try using action caching to have it only render the action once. 您可以尝试使用动作缓存使它仅呈现一次动作。 Then you can utilize cache sweepers to invalidate the cached js when you make any updates that would change the information in the js. 然后,当您进行任何会更改js中信息的更新时,可以利用缓存清除程序使缓存的js无效。

I think that really might be your best option. 我认为这确实是您的最佳选择。 Your going to go through a lot more trouble trying to get precompiled dynamic JS like that, especially if the content has a tendency to change at all. 试图获得这样的预编译动态JS会遇到很多麻烦,特别是如果内容有变化的趋势时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM