服务器端和客户端 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8476159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:34:27  来源:igfitidea点击:

Server-side and client-side JavaScript

javascriptnode.jsframeworks

提问by

I am having great difficulty choosing which frameworks to use for an app that is about to go into development. I am a front-end guy, and my friend is doing the back-end.

我很难为即将进入开发阶段的应用程序选择使用哪些框架。我是前端人员,我的朋友在做后端。

Say I was developing a simple todo list app. I have a template for each item on the todo list. Ideally, I would want the server-side JavaScript to use this template as well as the client-side JavaScript.

假设我正在开发一个简单的待办事项列表应用程序。我为待办事项列表中的每个项目都有一个模板。理想情况下,我希望服务器端 JavaScript 和客户端 JavaScript 一样使用此模板。

So on page load, if there are already 5 todos in the database, the HTML will be compiled on the server. If I then add a new todo item to the list, the client-side JS will compile the HTML using the same template.

所以在页面加载时,如果数据库中已经有 5 个待办事项,HTML 将在服务器上编译。如果我随后将一个新的待办事项添加到列表中,客户端 JS 将使用相同的模板编译 HTML。

I've heard a lot of buzz about Node.js, Backbone.js, etc. It is quite overwhelming just how many options there are for this sort of thing. Can anybody give me examples of using these technologies together?

我听说过很多关于 Node.js、Backbone.js 等的议论。对于这类事情有多少选择是非常令人难以抗拒的。谁能给我一起使用这些技术的例子?

采纳答案by alessioalex

Node.js is JavaScript on the server-side, while Backbone.js is used to structure your front-end stuff elegantly, using collections, models and views.

Node.js 是服务器端的 JavaScript,而 Backbone.js 用于优雅地构建前端内容,使用集合、模型和视图。

Each of the two has their own role. For a better comparison between front-end frameworks you can check Addy Osmany's TODO list, written in a LOT of them: https://github.com/addyosmani/todomvc

两者各有各的作用。为了更好地比较前端框架,您可以查看 Addy Osmany 的 TODO 列表,其中有很多:https: //github.com/addyosmani/todomvc

There are some nice tutorials over the net on Backbone also:
- http://dailyjs.com/2011/04/04/node-tutorial-19/
- http://backbonetutorials.com/
- http://net.tutsplus.com/tutorials/javascript-ajax/getting-started-with-backbone-js/

在 Backbone 网上也有一些不错的教程:
- http://dailyjs.com/2011/04/04/node-tutorial-19/
- http://backbonetutorials.com/
- http://net.tutsplus .com/tutorials/javascript-ajax/getting-started-with-backbone-js/

Here's an application that combines Node.js on the server with Backbone on the client:

这是一个将服务器上的 Node.js 与客户端上的 Backbone 相结合的应用程序:

http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/

http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/

Backbone (like Node.js) is really popular so you would get a lot of help / resources online.

Backbone(如 Node.js)非常流行,因此您可以在线获得大量帮助/资源。

回答by Mike

Paste the template file at the bottom of the page. This way your client code can use it easily without making a call to get the template.

将模板文件粘贴到页面底部。通过这种方式,您的客户端代码可以轻松使用它,而无需调用以获取模板。

An Example using JQuery when calling your template would be

调用模板时使用 JQuery 的示例是

<div style="display:none" id="sample_jquery_template">
  Hello ${name}
</div>

in your client side javascript code

在您的客户端 javascript 代码中

..javascript..
person = {name:'Joe'}
$.tmpl($("#sample_jquery_template").html(), person ).appendTo( "#destinationList" );

回答by Jorin

There are enough templating solutions out there like mustache.jsworking on both ends.
But for working with the templates on the client-side it is helpful to have the rendered data available.
Henrik Joreteg wrote a nice articleabout reusing your backbone-models and syncing them between client and server.

有足够的模板解决方案,比如在两端工作的mustache.js
但是为了在客户端使用模板,让渲染数据可用是有帮助的。
Henrik Joreteg 写了一篇关于重用主干模型并在客户端和服务器之间同步它们的好文章

回答by lee whitbeck

Dav Glass from Yahoo gave a good talkshowing how he ran YUI3 on the client and server with node.js.

来自 Yahoo 的 Dav Glass 做了一个很好的演讲,展示了他如何使用 node.js 在客户端和服务器上运行 YUI3。

Here is his githubof the examples from the video:

这是他的视频示例的github

You also might be interested in checking out jsdom

您可能也有兴趣查看jsdom

I just started learning node and this video really helped me see how to experiment on the server and client with node. You will see him disable javascript and the calendar still works - that was cool.

我刚刚开始学习 node,这个视频真的帮助我了解了如何使用 node 在服务器和客户端上进行实验。你会看到他禁用了 javascript 并且日历仍然有效——这很酷。

And here is a good backboneresource:

这是一个很好的主干资源:

回答by Bijan

You might have a look at http://derbyjs.com/#why_not_use_rails_and_backbone

您可能会查看http://derbyjs.com/#why_not_use_rails_and_backbone

They try to achieve the following when opening a webapp:

他们在打开 web 应用程序时尝试实现以下目标:

  1. Transmit a completely rendered page on the first request.
  2. From then on all changes shall be directly made client side and synced to the server via ajax.
  1. 在第一个请求时传输完全呈现的页面。
  2. 从那时起,所有更改都应直接在客户端进行,并通过 ajax 同步到服务器。

So usually the first request a user makes to a site with a "fat" client is pretty painful:

所以通常用户对带有“胖”客户端的站点的第一个请求是非常痛苦的:

  • App data has to be transferred and initialized
  • Data has to be loaded by the Client
  • Data is displayed
  • 应用程序数据必须传输和初始化
  • 数据必须由客户端加载
  • 显示数据

This behavior is usually way slower than transmitting an oldschool like server side rendered page. Gmail or iCloud for example need some time to load up because they do it like this.

这种行为通常比传输像服务器端呈现的页面那样的旧学校慢得多。例如,Gmail 或 iCloud 需要一些时间来加载,因为它们是这样做的。

回答by Juho Veps?l?inen

Sure. Check out TodoMVCto get a better idea of various alternatives.

当然。查看TodoMVC以更好地了解各种替代方案。