在 codeigniter 中处理大型 js 文件的最佳方法
我正在为我构建的应用程序之一构建一个统计包,我希望页面上有 8 个图表显示不同的信息。问题是 - 迭代 js 文件中的数据的最佳方法是什么? (或将 base_url() 添加到样式表中?
我将 js 保留在 root /scripts 中,
目前我有 8 个用于谷歌图表的图表 -
function drawtwitterChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'x');
data.addColumn('number', 'Twitter');
<?foreach($twitter_results->result_array() as $twitter_result){?>
data.addRow(["<?=$twitter_result['hour']?>", <?=goAssocArrayNumeric($twitter_result,0)?>]);
<?}?>
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':300,
'height':250};
new google.visualization.LineChart(document.getElementById('twitter_chart_div')).
draw(data, {curveType: "function",
width: 400, height: 320,
vAxis: {maxValue: 10}}
);
}
所有这些都在我的页面标题中,我想删除它并将其放入 js 中文件?仍然需要迭代数据 - 想知道其他人是如何做到这一点的?
i am building a stats pack for one of the applications i have built and i would like to have 8 graphs on the page showing different info. the problem is- whats the best way iterate through my data in a js file? (or add base_url() into style sheets?
i keep my js in root /scripts
currently i have 8 graphs with for my google charts-
function drawtwitterChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'x');
data.addColumn('number', 'Twitter');
<?foreach($twitter_results->result_array() as $twitter_result){?>
data.addRow(["<?=$twitter_result['hour']?>", <?=goAssocArrayNumeric($twitter_result,0)?>]);
<?}?>
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':300,
'height':250};
new google.visualization.LineChart(document.getElementById('twitter_chart_div')).
draw(data, {curveType: "function",
width: 400, height: 320,
vAxis: {maxValue: 10}}
);
}
all this is in the header of my page, i would like to remove this and place it into js files? need to still iterate through the data- wondering how others do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以解决这个问题。
最简单的方法是为动态生成的 JS 创建一个控制器。在调用输出 JS 的视图之前,设置响应头:
如果你有很多 JS 文件,你可以更进一步,让所有 JS 链接都调用这个控制器。您可以自定义链接以在一次调用中请求所有 JS 文件,并且控制器可以将它们组合起来并将它们作为单个文件提供服务。您可以在请求中包含版本信息,以确保您获得正确的版本并防止在需要更新时 JS 被缓存。
更进一步,控制器可以缓存给定版本的组合文件,这样您就不必在每次请求时都处理它。如果你有需要动态内容的JS文件,你可以对JS进行分层,将静态JS文件合并起来缓存,然后每次生成动态JS文件,与缓存的静态文件合并,然后提供服务。
There are a few ways to approach this.
The simplest would be to create a controller for your dynamically generated JS. Before calling the view that outputs the JS, set the response header:
If you have lots of JS files, you can take it even further by making all of your JS links call this controller. You can customize the links to request all of your JS files in a single call, and the controller can combine them and serve them as a single file. You can include version info in the request to make sure you get the correct version and prevent JS from being cached when you need to update it.
Taking it even further, the controller can cache the combined files for a given version so you don't have to process it each time it is requested. If you have JS files that need dynamic content, you can tier your JS so that static JS files are combined and cached, then the dynamic JS files are generated each time, combined with the cached static, and then served.