Web Services – Is This an Anti-Pattern or Clever Design?

anti-patternsrestweb services

I've basically stared to do the following when creating a REST service:

  1. HTML is requested
  2. service returns the desired web page but without the requested "resource", eg. data
  3. web page contains JavaScript that issues AJAX request to the same service (different content-type)
  4. service then returns the actual data (JSON) and the page displays it

On one side it seems inefficient (2 requests) but then were I used this, "performance is of no concern", meaning low traffic internal app and the web sites are simple and load fast.

The reason I ended up with this is that the web page can then be almost pure Html + JavaScript and almost no server-side stuff is required, especially no loops, to create tables and stuff like that (which I think is very ugly compared to things like slickgrid), e.g. separation of data and view.

Now before I get to using this, is this a good idea or should I just stop doing it?

Best Answer

If you request a resource and it does not contain the data, than it's not REST service. The service providing the actual data in json might be, but the HTML part is not. For a web application it does not matter.

The technique works, but you need to be aware of it's limitations:

  1. Search engines don't interpret JavaScript, so site implemented like that won't be indexable by Google and the likes. For internal application it does not matter, for public facing one it would matter much.
  2. Users with special needs (like those using Braille terminals) use special browsers that are rather limited and may not interpret the JavaScript properly.

I would also note that the code generating the HTML is basically the same whether it runs server-side or client-side. You have much bigger choice of both languages and frameworks on server-side and I am sure there are several equivalents of slickgrid too.

You can, and should, still maintain separation of data and display on the server side. The template system can, and should, simply take the data as data structure or even json (especially if the actual service is in different language than the template system) and just expand a template with that data.

And no, I am not thinking about PHP; it's the least capable template system out there (though there are some better ones built on top of it). I am thinking Genshi or XSLT or something even more advanced that provides web widgets.

Related Topic