Rendr - Use Backbone on both the server and client
github.com
github.com
Of course, it's not like rendr has any real documentation, either.
However it has been working very well for us, so it is validation of the rendr approach.
A quick glance at their documentation indicates a very similar design, comparing the two projects should be instructive.
Here's a typical ViewModel definition (in JS):
foo.vm.Resource = highbrow.ViewModel.extend({
lastUpdated: function() {
return moment(this.model.get('modifiedAt')).fromNow();
}
});
foo.vm.Resource.attrs(['name', 'content']);
This shows the main two features of ViewModel's: a whitelist of attrs that the template can access directly, and a set of functions that would otherwise end up polluting your model with view side code.This decoupling dramatically improves the structure of our applications, without introducing Rails style controllers which are also totally misnamed in my opinion. Rails controllers are essentially two things: routers and MV-shims. Splitting the two makes for much improved program structure.
1: https://github.com/airbnb/rendr/blob/master/shared/base/view...
2: https://github.com/wvl/highbrow/blob/master/src/view-model.c...
* Minimize if (server) {...} else {…}
was unfortunate. A grep of the highbrow source code indicates about 10 instances of that pattern, so I don't think there's a need for a separate base/server/client directory structure like rendr has, I think that creates artificial distinctions. It may be because I'm used to reading the highbrow source (because that's the only documentation :), but I find those if/else blocks to be very illuminating.If I go directly to /search/q=hello, the request will go to server, it will render the HTML on server and send it to client. What happens next? Do they send all the Javascript + client-side templates alongside as well so that further requests are catered on client?
Think I should fiddle around with the sample app.
Yes.
I remember chatting with Spike about this at a Backbone.js meetup at Airbnb's office last year, I think they were aware of the limitations similar frameworks were running into.
I'd be interested in seeing how this play outside of Airbnb's use case, but from experience I think sharing backbone.js on both client and server, while neat, end up being pretty constraining.
I love the Rendr approach, yet we feel that our backbone-serverside framework provides an even more flexible way to tackle the same issue.
Read more at SC5 blog (http://blog.sc5.fi/2013/04/serving-backbone-for-robots-legac...) or at the Mozilla Hacks article: https://hacks.mozilla.org/2013/04/serving-backbone-for-robot...
Rendr looks like a much more robust solution, however. I'll have to see if I can incorporate some of it's techniques, specifically serving HTML on page load.
So you still need both backbone & mongoose/whatever validations if you want them both on the client & server side. In practice, the system is fast enough that server side validations may be sufficient.
Given my experience with the very similar highbrow, I expect that rendr can return a fully functional page in 10-20 ms. In a typical client-side JS framework, the first page takes 300-3000 ms to render. Subsequent pages are lightning quick, but in many cases it's the first page that's the important one.
But seriously, looks very much like Shopify's batman.js http://batmanjs.org
I think most of the similarities you see stem from those two points.
But there are some massive differences:
- batman.js is Rails-style MVC, rendr is based on Backbone so you must supply your own controllers. A good argument can be made that Rails is not truely MVC, but it at least attempts to do so. rendr is MV.
- RENDR runs and renders on the server. batman.js relies on a Rails or REST app on the server.
- rendr is based on Backbone.js and Express, batman.js isn't
My interpretation of the key benefits: large speed increases for users, and vastly improved SEO.
Great job!
Though, don't write your business code in javascript. Hell to maintain.
If your definition of the JSON to HTML transformation is declarative, it makes sense to allow the server to do it for the data that's available to it.
The web has a semi-long history of "dumb clients," and that means the server has to be able to do the rendering. There are some pretty good reasons for this, and projects like Rendr try to make it as easy as possible. Seems good to me!
> Talk to RESTful API
> Simple Express middleware
Hard times for Meteor