js的加载初始数据的最佳方法

js的加载初始数据的最佳方法

本文介绍了是什么在Backbone.js的加载初始数据的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Backbone.js的,模型加载和保存是通过Ajax调用完成。然而,在那里装载在页面加载初始集合,而无需通过AJAX拉下来的最佳做法?我试图做尽可能多的服务器端渲染尽可能锋线。

In Backbone.js, model loading and saving is done via ajax calls. However, are there any best practices to loading the initial collection on page load without having to pull this down via ajax? I'm trying to do as much server side rendering as possible up front.

在过去,我已经播种了包含初始数据状态​​的JSON字符串,因此它可以被渲染服务器端JavaScript变量的HTML,但我不知道这是一个很好的做法。

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的对象的(不是JSON无欲无求的HTML这种方法的字符串像你描述它的吧?)当然是我的$ p $做初始加载pferred方式。不仅为(明显)的原因,它消除了等待最初的AJAX调用返回的延迟,也因为少了一个打开的连接释放浏览器加载别的东西,而不是(像IMG SRC或诸如此类的东西),让你稍微早document.onLoad。

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. 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.

它的建议是,使用这种方法时,你把在脚本标签的所述变量在主体的底部(即不是在头部部分),为了给在页面上的静态HTML元素的机会加载和渲染第一。 document.onLoad火灾时,JSON数据已准备就绪。

It's recommended that, when using this method, you put the said variable in a script tag at the bottom of the body (i.e. not in the head section), in order to give the static html elements on the page a chance to load and render first. The json data is ready when document.onLoad fires.

这篇关于是什么在Backbone.js的加载初始数据的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:11