Understanding the job of routes in Node.js (api) when utilizing Backbone.js (frontend) -


i beginning learn using backbone.js. have used web framework built on top of node.js handle routes , responses. backbone possibility of spa (single page application).

i believe question related one: account backbone.js pushstate routes node.js express server? (a question of express.js + backbone).

in code given:

app.get('/', function(req, res) {     // trigger routes 'domain.com' , 'domain.com/#/about'     // here render base of application });  app.get('/about', function (req, res) {     // trigger toure 'domain.com/about'     // here use templates generate right view , render }); 

from using node web frameworks have not used json requests data, have queried database in route closure. job of node.js (in node+backbone environment) serve backbone page , not query database? directs clients specified backbone.js template without passing data, , backbone takes over?

so if wanted display book models (example.com/books) instance, send user via node url, , backbone take care of querying database (with model, of course)? code like?

most of backbone tutorials i've seen have dealt external api's. thanks!

so single page apps, need consider 2 types of pages: full-page loads , single-page routes. full page load entry point when user first arrives on site, should consider deep linking uri within site browser refresh of current uri.

the easy approach

  • don't query database in node.js routes
  • just return "start page" html
  • in browser, backbone router initialize views/collections/models , fetch data need server , render page in browser.

get working first , understand ins , outs because overall it's least complicated. cause delay in perceived performance page won't usable until 2nd round trip server load data complete. fact start page html may void of interesting search engine crawler data seo may important factor (or not).

the server side bootstrap

so official guidance jeremy ashkenas full page load, server should:

  • query database based on route , request parameters
  • generate full html document page, including rendered views
  • emded necessary data json in html document , use bootstrap backbone models when backbone app loads on client
    • some how wire bootstrapped backbone objects dom (this left exercise reader)

the main advantage here perceived performance, seo (which may or may not matter you). however, implies existence of sophisticated framework enable render identical html in both node.js , in browser, backbone not provide. there project called rendr implements concept of uniform html rendering across browser , node, may consider using or @ least studying source inspiration.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -