我应该呈现使用JavaScript或服务器这个模板? [英] Should I render this template using JavaScript or the server?

查看:98
本文介绍了我应该呈现使用JavaScript或服务器这个模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在渲染一个新闻源。

I'm rendering a news feed.

我打算使用Backbone.js的为我的javascript的东西,因为我生病了上工DOM结合,将JQuery的的。

I'm planning to use Backbone.js for my javascript stuff because I'm sick of doing manual DOM binds with JQuery.

所以现在我期待在2个选项。

So right now I'm looking at 2 options.


  1. 当用户加载页面时,新闻动态集装箱是空的。但页面会触发这使得新闻源的项目到屏幕上一个javascript。这将扎入骨干的模型和收藏品等。

  1. When the user loads the page, the "news feed" container is blank. But the page triggers a javascript which renders the items of the news feed onto the screen. This would tie into Backbone's models and collections, etc.

当用户加载页面时,新闻提要是由服务器呈现。即使JavaScript是关闭,项目将仍然显示,因为服务器通过模板引擎渲染它。

When the user loads the page, the "news feed" is rendered by the server. Even if javascript was turned off, the items would still show because the server rendered it via a templating engine.

我想用Backbone.js的保持我的javascript干净。所以,我应该选择#1,正确的?但是#1比#2要复杂得多。

I want to use Backbone.js to keep my javascript clean. So, I should pick #1, right?? But #1 is much more complicated than #2.

顺便说一句,我问这个问题的原因是因为我不希望使用Backbone.js的路由功能。我想单独加载每个页面,并且使用骨干为页面的各个项。换句话说,我使用Backbone.js的一半。

By the way, the reason I'm asking this question is because I don't want to use the routing feature of Backbone.js. I would load each page individually, and use Backbone for the individual items of the page. In other words, I'm using Backbone.js halfway.

如果我是使用Backbone.js的路由功能,那么明显的答案是1,对不对?但恐怕要花太多的时间来建立路由系统,并且时间要平衡到我的方程为好。

If I were to use the routing feature of Backbone.js, then the obvious answer would be #1, right? But I'm afraid it would take too much time to build the route system, and time should be balanced into my equation as well.

我很抱歉,如果这个问题是困惑:我只是想知道Backbone.js的使用,节省时间以及最佳实践

I'm sorry if this question is confusing: I just want to know the best practice of using Backbone.js and saving time as well.

推荐答案

有优点和缺点都,所以我要说的是:选择最适合的的选项,按的要求。

There are advantages and disadvantages to both, so I would say this: choose the option that is best for you, according to your requirements.

我不知道Backbone.js的,所以我要保持我的回答对客户机 - 服务器与端呈现。

I don't know Backbone.js, so I'm going to keep my answer to client- versus server-side rendering.

这个方法可以让你快速渲染架构在服务器端,然后让用户的JavaScript拿起实际内容。

This approach allows you to render your structure quickly on the server-side, then let the user's JavaScript pick up the actual content.

优点:


  • 更快感知的用户体验:如果有初始足够的静态内容呈现,那么用户得到他们的页面返回(或至少它的开始)更快,他们不会被人打扰约动态内容,因为在所有的可能性,将呈现快速合理了。

  • 缓存更好地控制:通过要求浏览器发出多个请求,你可以设置你的服务器使用不同的缓存头每个网址,根据您的要求。通过这种方式,可以让用户高速缓存初始页面渲染,但要求用户每次获取动态(变化)的内容。

  • Quicker perceived user experience: if there's enough static content on the initial render, then the user gets their page back (or at least the beginning of it) quicker and they won't be bothered about the dynamic content, because in all likelihood that will render reasonably quickly too.
  • Better control of caching: By requiring that the browser makes multiple requests, you can set up your server to use different caching headers for each URL, depending on your requirements. In this way, you could allow users to cache the initial page render, but require that a user fetch dynamic (changing) content every time.

缺点:


  • 用户,必须启用JavaScript :这是一个明显的例子,我不应该甚至需要提到它,但你切割出你的用户群的(很小)部分,如果你不'T提供一个优美的替代你的JS-沉重的网站。

  • 复杂:这一个是有点主观的,但在某些方面它在你的服务器端语言的一切,并不需要这么多回,来回只是简单。当然,也可以是双向的。

  • 慢后处理:这取决于浏览器,但事实是,如果有很多的DOM操作或其他后处理的需要检索动态内容后发生,它可能会更快让服务器做,如果服务器没有得到充分利用。大部分浏览器都擅长基本DOM操作,但如果你所要做的JSON解析,分类,算术等,一些可能是服务器上更快。

  • User must have JavaScript enabled: This is an obvious one and I shouldn't even need to mention it, but you are cutting out a (very small) portion of your user base if you don't provide a graceful alternative to your JS-heavy site.
  • Complexity: This one is a little subjective, but in some ways it's just simpler to have everything in your server-side language and not require so much back-and-forth. Of course, it can go both ways.
  • Slow post-processing: This depends on the browser, but the fact is that if a lot of DOM manipulation or other post-processing needs to occur after retrieving the dynamic content, it might be faster to let the server do it if the server is underutilized. Most browsers are good at basic DOM manipulation, but if you have to do JSON parsing, sorting, arithmetic, etc., some of that might be faster on the server.

这种方法允许用户一次接受一切,也迎合了那些不具备良好的支持JavaScript的浏览器,但它也意味着一切都需要更长的时间浏览器获得的第一个&LT之前; HTML方式> 标签

This approach allows the user to receive everything at once and also caters to browsers that don't have good JavaScript support, but it also means everything takes a bit longer before the browser gets the first <html> tag.

优点:


  • 内容出现一次全部:如果您的服务器速度快,它都将立刻呈现的一切,就是这样。没有杂乱XmlHtt prequests(有没有人仍然直接使用那些?)。

  • 快速后处理:就像你不希望你的应用程序层做一个数据库查询集的排序,因为数据库是快,你可能也想保留在处理一个良好的金额服务器端。如果你设计的客户端的方式,很容易得意忘形,把处理错了地方。

  • Content appears all at once: If your server is fast, it will render everything all at once, and that's that. No messy XmlHttpRequests (does anyone still use those directly?).
  • Quick post-processing: Just like you wouldn't want your application layer to do sorting of a database queryset because the database is faster, you might also want to reserve a good amount of processing on the server-side. If you design for the client-side approach, it's easy to get carried away and put the processing in the wrong place.

缺点:


  • 慢感知的用户体验:用户将无法看到一个字节,直到服务器的工作就全部完成。当然,服务器可能会通过它来压缩,但它仍然是一些额外秒钟在用户侧,你会用什么样的渲染,你可以马上。
  • 请他们帮忙
  • 因为服务器花费更多的时间上的请求不结垢以及:这可能是你真的希望服务器能够快速完成的请求,并移动到下一个连接

  • Slower perceived user experience: A user won't be able to see a single byte until the server's work is all done. Sure, the server is probably going to zip through it, but it's still a few extra seconds on the user's side and you would do them a favor by rendering what you can right away.
  • Does not scale as well because server spends more time on requests: It might be that you really want the server to finish a request quickly and move on to the next connection.

哪些是最重要的你的要求?这应告知你的决定。

Which of these are most important to your requirements? That should inform your decision.

这篇关于我应该呈现使用JavaScript或服务器这个模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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