2

So I have decided to take a project I have been working on, and project management system, and convert it from a standard multi-paged application to a single-page application. In the process I have looked at a number of javascript MV*ish frameworks (ember, knockout, backbone, etc...) and have decided to go with backbone.js as it seems the most flexible solution and that I already use underscore.js for both its utilities and template system.

The one biggest concern I have with doing this is the potential code duplication I am going to have for model and business/domain logic in both my PHP models and in my backbone models.

This is just a technical cost I have pay when going with an architecture like this or are there some things I can do to lessen this concern?

ryanzec
  • 27,284
  • 38
  • 112
  • 169

2 Answers2

3

You may want to take a look at a previous question I answered involving Node.js:

Reusing backbone views/routes on the server when using Backbone.js pushstate for seo/bookmarking

What I'm currently doing right now is using Davis.js + Mustache + Java Spring MVC backened (based on my original question: Single page Web App in Java framework or examples?).

If the browser does not support Pushstate then I have the server do the entire rendering of the page using a Java version of Mustache (ie standard Web 1.0). If the browser does support Pushstate then the browser will make an AJAX request for a JSON version of the model. The model is then rendered client side use icanhz (ie javascript mustache).

This works fairly well if a large part of your logic is getting a model an then rendering it based on specific URL. That is your business logic is "based on some URL I'm going to render this". This is how most of the sites on the web work (including this one which is still rather web 1.0). Obviously this would not work for say something like real time chat or HTML5 game.

A python version of this design is mentioned here: http://duganchen.ca/single-page-web-app-architecture-done-right/

I'm sure someone has done a PHP version.

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Diffidently some good information here but after read more on this subject, I think I am going to try to keep as much business logic on the server side, a number of them pointed out here : http://programmers.stackexchange.com/a/114800/20237. The only thing I hope to have to duplicate in validation, most everything else should probably be keeped on the server code. Also I can't use mustache as I really need a template engine that supports true conditionals (like if(someVar == 'some value')) like the underscore template engine does. – ryanzec Apr 17 '12 at 16:16
  • If you need more logic for your templates you can try to use handlebars which is an extension of Mustache. I have just learned to except the limitations and add as much to the model as I can to help render (in somes cases even adding html to the model). – Adam Gent Apr 17 '12 at 16:52
  • You can still do 'true conditionals' with mustache templates, you just don't do them in the template itself, rather you have a view class/object and implement your logic there. This way it is much easier to test etc. – Oliver Nightingale Apr 30 '12 at 21:43
1

If you are using different languages in server and in client I think there is not possibility to avoid this partial logic duplication you are concern on.

If you definitely want to use same code in server and client you have to move all of it to the only common language: JavaScript.

There are multiple JS frameworks those integrate very transparently the development between server and client: derby, meteor, ...

fguillen
  • 36,125
  • 23
  • 149
  • 210