在加载HTML页面加载一个Backbone.js的集合 [英] loading a backbone.js collection on html page load

查看:134
本文介绍了在加载HTML页面加载一个Backbone.js的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Backbone.js的web应用程序的架构问题。比方说,你要创建一个电影收藏的web应用。你会使用一部电影模型和收集你的电影收藏。

I have a question about the architecture of a backbone.js web app. Let's say you are creating a movie collection webapp. You would use a Model for a movie and a Collection for your collection of movies.

对于集合的观点基本上是李时珍的负载。

The view for the collection is basically a load of li's.

为了使由谷歌可索引的网页,你会允许它没有JavaScript的工作,并在服务器端得到呈现。所以这将mydomain.com/12~~V将在服务器端建立该集合在html里的元素。

In order to make the webpages indexable by google you would allow it to work without javascript and get rendered on the server side. So that going to mydomain.com/12 would on the server side build up the html li elements for that collection.

如果你再想要使用Backbone.js的修改集,你就需要将数据加载到骨干网。什么是做到这一点的最好方法是什么?两种方式我能想到这样做的是...

If you then wanted to use backbone.js to modify the collection, you would need to load the data into backbone. What is the best way to do this? Two ways I can think of doing this is...

1)使用jQuery遍历李元件读取数据到骨干 - 与此问题是,HTML可能只包含电影的名字,而不是可能在骨干模型需要电影的ID /主管信息。我想这些数据可以被包含在HTML和CSS使用隐藏的。

1) use jquery to iterate over the li elements reading the data into backbone - the problem with this is that the html probably only contains the movie names, and not the movie id's /director information that might be needed in the backbone model. I suppose that data could be included in the html and hidden using css.

2)扔掉一切都在列表中,并用ajax从服务器重新加载 - 这对我来说似乎是因为所有的数据都已经在第一页加载派出浪费

2)throw away everything in the list and reload it from the server using ajax - which to me seems like a waste since all the data could have been sent on the first page load.

要么是这些方法好的做法呢?如果没有,应该怎样做呢?

Are either of these methods good practice? If not, how should it be done?

推荐答案

杰里米阿什克纳斯,骨干作者,建议您打印的页面JSON模型数据,并加载它作为一个对象:

Jeremy Ashkenas, author of Backbone, recommends that you print the model data as JSON in the page, and load it as an object:

<script>
  Accounts.reset(<%= @accounts.to_json %>);
  Projects.reset(<%= @projects.to_json(:collaborators => true) %>);
</script>

请参阅骨干常见问题 - 加载自举模式

这篇关于在加载HTML页面加载一个Backbone.js的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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