简体   繁体   English

在Backbone.js中加载初始数据的最佳方法是什么?

[英]What's the best approach to load initial data in Backbone.js?

In Backbone.js, model loading and saving is done via ajax calls. 在Backbone.js中,模型加载和保存是通过ajax调用完成的。 However, are there any best practices to loading the initial collection on page load without having to pull this down via ajax? 但是,是否有任何最佳实践在页面加载时加载初始集合而不必通过ajax将其拉下来? I'm trying to do as much server side rendering as possible up front. 我正在尝试尽可能多地进行服务器端渲染。

In the past, I've seeded the html with a javascript variable containing a json string of the initial data state so it can be rendered server side, but I'm not sure if this is a good practice. 在过去,我已经使用包含初始数据状态的json字符串的javascript变量播种html,因此它可以呈现给服务器端,但我不确定这是否是一个好习惯。

Don't know if it's necessarily the best practice, but this method of seeding the html with a json object (not a json string as you described it, right?) is certainly my preferred way of doing initial loading. 不知道它是否是最好的做法,但这种用json 对象播种html的方法(不是你描述的json 字符串 ,对吗?)当然是我做首次加载的首选方法。 Not only for the (obvious) reason that it removes the delay of waiting for the initial AJAX call to return, but also because the one less open connection frees the browser to load something else instead (like an img src or whatnot), getting you to document.onLoad slightly sooner. 不仅因为(显而易见的)原因,它消除了等待初始AJAX调用返回的延迟,而且因为一个较少开放的连接释放了浏览器以加载其他东西(如img src或诸如此类),让你稍微提交一下document.onLoad。

It's recommended that, when using this method, you put the said variable in a script tag at the bottom of the body (ie not in the head section), in order to give the static html elements on the page a chance to load and render first. 建议在使用此方法时,将所述变量放在正文底部的脚本标记中(即不在head部分中),以便为页面上的静态html元素提供加载和呈现的机会第一。 The json data is ready when document.onLoad fires. 触发document.onLoad时,json数据就绪。

From Backbone docs , initialising models in script tag is not a bad practice. Backbone文档中 ,在脚本标记中初始化模型并不是一个坏习惯。 In my current project I decided to set only plain objects inside window.projectData , to be able to init Backbone models in external javascripts. 在我当前的项目中,我决定只在window.projectData设置普通对象,以便能够在外部javascripts中初始化Backbone模型。

<script>
  ;(window.projectData || (window.projectData = {})).modelName = {/* value */};
</script>

The approach you select will probably depend on how much data you're planning to load and how that data will be used within the page. 您选择的方法可能取决于您计划加载的数据量以及该数据在页面中的使用方式。

If most of the required data will not ultimately end up rendered on the page, bootstrapping the initial state into a javascript variable might not be a bad route. 如果大多数所需数据最终不会最终呈现在页面上,则将初始状态引导到javascript变量可能不是一个糟糕的路径。

If the data is directly related to the presentation of the page, however, you might prefer to parse a pre-rendered DOM for the initial state of the backbone application. 但是,如果数据与页面的呈现直接相关,您可能更愿意为骨干应用程序的初始状态解析预呈现的DOM。 The obligatory word of caution is that this kind of parsing will be somewhat slower, especially for large data sets. 必须提醒的是,这种解析会稍微慢一些,特别是对于大型数据集。

If you do end up opting to parse pre-rendered content, I put together a small jQuery DOM parser a while back that might prove useful for mapping the presented content into a Backbone-ready form. 如果你最终选择解析预先渲染的内容,我会在一段时间之后整理一个小的jQuery DOM解析器 ,这可能对将所呈现的内容映射到Backbone-ready表单有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM