Progressive enhancement - Node.js, Backbone.js

被刻印的时光 ゝ 提交于 2019-12-11 03:51:26

问题


The node.js server has two roles:

  • RESTFul API on routes with prefix /api
  • Renders website pages on the others routes (/ /plans /features /terms ...)

For the moment, all my pages render a "Loading page..." combined with the Backbone application that replaces the DOM when the Backbone.router starts.

I would like to build the website pages server-side when an user reaches a page and let Backbone handles the next part of the navigation when the user navigates on the website.

I know how to do it server-side but client-side when a page is loaded with the DOM already built, the Backbone.router loads and then replaces the DOM because it does not know that the view is already preloaded.

How to fix it client-side?

Code:

  • The router: http://pastebin.com/aUuXaVm9

  • Home view: http://pastebin.com/qS1tHUfq

  • Terms view: http://pastebin.com/et1mrbLK

Update: the new code: https://gist.github.com/mathieug/d50c861e63dd647f1c2b
Now I need the runSlider method to be called at the first load.


回答1:


When you start the History, make sure to pass {silent: true} as an option to let Backbone know that you've already loaded a complete page. This will prevent the router from replacing the DOM when the view is already preloaded (the first time).

From the Backbone.js docs:

If the server has already rendered the entire page, and you don't want the initial route to trigger when starting History, pass silent: true.

So, your code should look like:

Backbone.history.start({pushState: true, silent:true});



来源:https://stackoverflow.com/questions/24600991/progressive-enhancement-node-js-backbone-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!