模板渲染与node.js的Backbone.js的和 [英] template rendering with node.js and backbone.js

查看:145
本文介绍了模板渲染与node.js的Backbone.js的和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人找到了一个很好的解决方案,用于开发模板可在服务器和客户端上使用Backbone.js的?

Has anyone found a good solution for developing templates for backbone.js that can be used on the server and the client?

这是与Backbone.js的历史堆栈很令人满意,因为用户可以分享并链接到浏览器地址栏真实的URL和Node.js的服务器可以呈现在第一页视图页面,同时使用相同的模板在客户端重建在后续页面访问量页面。

This is really desirable with the backbone.js history stack, as users can share and link to real urls in the browser location bar, and the node.js server can render the page on first page view, while using the same templates in the client to rebuild pages on subsequent page views.

这也将为用户和搜索引擎蜘蛛的联系,不会有解析或执行JavaScript来看看一个完全渲染和工作页面的理想输出。

This would also provide an ideal output for both users and for search engines that spider the links and won't have to parse or execute javascript to see a fully rendered and working page.

更新了更多的信息:

两种可能的途径来这似乎是:

Two possible approaches to this appear to be:

1)骨 - https://github.com/developmentseed/bones

骨骼有一些怪癖安装,目前需要的是旧版本节点和新公共管理的。

Bones has some quirks to install and currently needs an old version of node and npm.

2)胶囊 - https://github.com/andyet/capsule

我没有试过呢,但似乎相似。我有兴趣,如果任何人有任何这些项目的兴趣。

I haven't tried this yet, but it seems similar. I'd be interested if anyone has interest with either of these projects.

推荐答案

我目前工作的一个的命名为一code没有你要的那种什么框架。目前,它缺乏的文档,但我有一个基于其工作项目,所以它可以为你工作了。我也期待贡献者。

I'm currently working on a framework named "onecode" that does what you asked for. Currently it lacks documentation, but I have a working project based on it, so it could work for you too. I'm also looking for contributors.

下面是它如何工作的。几乎所有code是客户端和服务器,包括模型和视图之间共享。

Here's how it works. Almost all code is shared between client and server, including models and views.


  1. 在服务器上,创建一个REST API,可以定义业务规则,安全性,数据库操作,所有你不能信任到客户端。

  2. 这个API是从客户端(使用标准的骨干Ajax调用)同时使用,并从服务器本身当第一次请求的页面(直接使用覆盖 $。阿贾克斯法)

  3. 当客户端请求页面时,服务器将创建所有需要的模型和视图,使得直接请求API和呈现HTML。此外,它会记住从API调用来所有的数据和HTML元素对应到视图。

  4. 模型/视图code,HTML和数据供应给客户。在这里,HTML是完全渲染和功能,这样即使用户关闭JavaScript的,他可以​​点击链接和浏览网页(他不会得到当然任何动态功能)。但是,如果启用了Javascript中,所有的模型和视图重新创建和重新绑定到DOM自动在后台节点,赚不到用户等待。

  5. 之后,应用程序的工作就像一个单页的应用程序,从相同的API请求仅数据(JSON),在客户端呈现模板。

这意味着:


  1. 您写的presentation和动态code只有一次。

  2. 第一个请求的页面供应并显示用户闪电般迅速,不需要等待所有脚本加载和运行,只是HTML / CSS。

  3. 下一页页也非常快,因为只有原始数据被请求时,模板上的客户端呈现。你也可以把它视觉吸引力,而不是通常的页面重载。你甚至可以播放音乐的同时浏览用户的网站。

  4. 搜索引擎和社交网络爱你。

该架构迫使一些理智的要求,这将使你一个更好的开发。是这样的:

The architecture forces some sane requirements which will make you a better developer. Something like:


  1. 需要服务器操作和业务规则独立的,定义良好的API。

  2. 没有全局变量。

  3. 查看被视为更严格的比一般的骨干,更像是可堆叠的UI组件。

  4. HTML渲染和动态行为之间的明确分离。

有一个很简单的例子可以发现,在源代码树。我使用的骨干作为模型和视图,并 Browserify 提供的js包,客户端的基础。

A very simple example can be found in the source tree. I use Backbone as a basis for Models and Views, and Browserify deliver js package to client.

在我的项目中我用EJS联模板作为模板引擎。这样做的优势,在同一个地方保持HTML和code(我用的CoffeeScript)的。但该框架能够从外部文件模板的包装,像翡翠其它模板引擎。请看到一个模板化的例子就可以了怎么可能的完成。

In my project I use inline templates with EJS as a templating engine. This has advantage of keeping HTML and code (I use CoffeeScript) in the same place. But the framework is capable of packaging templates from external files, with other templating engines like Jade. Please, see a templating example on how it could be done.

请让我知道如果你有兴趣这种方法,也许它会推我开始为它编写的文档。

Please let me know if you are interested this approach, maybe it'd push me to start writing docs for it.

这篇关于模板渲染与node.js的Backbone.js的和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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