Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-using backbone routes, models, and views on the node.js server

I am trying to build an application which both works as a SPA and as a normal application without re-writing any of the code as much as possible. My research has led to believe that this could be very possible using node + backbone (on both the server and client). I have found some resources on how to do this but nothing truly complete. Have anyone tried a full solution to re-using all the code?

The resources that I found: Re-using backbone models on the server: http://blog.andyet.com/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/

Re-using backbone views and layout manager on the server: http://vimeo.com/46033641

I have also found some frameworks that achieve this such as (bones, derby, meteor), but I dont feel like using such a framework is the best solution since I still want to have full control over backbone and node.

like image 791
Marwan Roushdy Avatar asked Jan 10 '13 13:01

Marwan Roushdy


2 Answers

What you are trying to implement is Resource-View-Presenter model. Please read this article completely http://blog.nodejitsu.com/scaling-isomorphic-javascript-code. It provides much needed insight for isomorphic code that can execute both on the client and the server. It also helped me understand various models and architectures.

What backbone.js uses is traditional MVC framework, which cannot work on both client and server without modifications. So if you want to stick to backbone.js you will face this disadvantage sooner or later. I tried backbone too and found no easy way to do that.

I tried looking for RVP frameworks on node.js, but it is still new. I would suggest you look into flatiron.js http://flatironjs.org/. It is based on Resource-View-Presenter model, the only one I could find for node.js. Some changes will be required to implement on both server/client, but it is not much. You will find it is similar to backbone.

Hope it helps you to find the right path.

like image 189
user568109 Avatar answered Sep 21 '22 08:09

user568109


I recently did some research on the field and found some interesting projects that are using the same backbone code on the client and server (nodejs).

Project 1

The first one was introduced by Keith Norma from Groupon. He managed to reuse backbone router + templates on the client and server.

App demo: https://github.com/keithnorm/SpainJS-Pipedream-Demo

Presentation at SpainJS talk: http://www.youtube.com/watch?v=jbn9c_yfuoM

Project 2

The second project is Rendr presented by Spike Brehm from Airbnb but not released yet to the public. It seems a bit more ambitious as it also trades backbone model and collections to the nodejs server. You can read more about it here: http://nerds.airbnb.com/weve-launched-our-first-nodejs-app-to-product

UPDATE: Rendr code has been released: https://github.com/airbnb/rendr

Project 3

There is another project that also allows to share backbone code between client and server: https://github.com/developmentseed/bones

like image 25
Daniel Fernandez Avatar answered Sep 23 '22 08:09

Daniel Fernandez