Backbone: Dealing with stateful applications (part 1)
pau.calepin.co
pau.calepin.co
* You usually want to avoid having multiple client-side models for a single database row. Keeping your client-side schema close to your database avoids mapping headaches later.
* When navigating around, be sure to cancel outstanding ajax requests if the user clicks on another link before the response arrives.
* Lazy-loading of associated models is handy, described further here:http://backbonejs.org/#FAQ-nested
... and a few more tips:
* You don't need to use an event when a simple callback will do.
* In terms of JS apps, Tim Toady is your friend: http://backbonejs.org/#FAQ-tim-toady
* You often aren't modeling the complete server-side state in the browser, so look for opportunities to simplify by glossing over data that only concerns the backend.
Here's hoping the next post comes soon!
Locking data to prevent this is a usability problem. Pushing changes to the each client is a partial solution, but then you're faced with the users watching the data change in front of them, without any action on their part. This is especially a problem in complex applications with multistep workflows.
Perhaps it is outside the scope of this series, since this appears to be focused on the technical aspects of using Backbone rather than application design. But a discussion of patterns that overcome these problems would be very useful.
I can almost see some sort of DAG-type (like git) versioning possibly working; or maybe where the only changes that are saved to the store are properties that only one client modified.
It seems as though you would need to specify what groups of attributes need to be modified atomically. Say you have a user model, for example; an address comprised of multiple fields should probably be modified as a unit, but the address and email could be modified simultaneously. This might correspond to how you would logically divide your models anyway. Merges could then be allowed as long as these units are intact.
Doing this while maintaining usability seems like a tremendous challenge, however, and I'd love to hear ideas for doing this in a way that's painless for the user, especially when there are dependencies between models.
http://documentcloud.github.com/jammit/
Just the templating bit: http://documentcloud.github.com/jammit/#jst
... which allows us to include all of our templates in our JavaScript asset packages by listing them like this:
https://github.com/documentcloud/documentcloud/blob/master/c...
... which then means that they'll be served as part of the final, minified, compressed "core" asset package here:
http://www.documentcloud.org/assets/core.js (bottom of the file)
Or, as a standalone asset here:
This is what we use: https://github.com/teambox/trimmer
In a nutshell, it takes everything under app/templates and compiles it from Rails (so it has access to I18n and ruby) and serves jades, which we later compile and minify.
Trimmer serves the Template and I18n objects, as you can see on this compiled file:
https://d238xsvyykqg39.cloudfront.net/assets/trimmer-en-58fc...
I like that your solution actually packs it together with the rest of the code, avoiding one extra request for templates. Maybe we'll add that to our system too.
What they need is a simple, to the point, complete examples of apps. There's one that I found that makes some sense to me:
And when there isn't an existing example of what I'm trying to do close at hand, I find the Backbone docs to be quite good.
What worked the best (however not optimal) for me was to combine the techniques used in the PeepCode(https://peepcode.com/products/backbone-js) screencasts and Thoughbot's book (https://workshops.thoughtbot.com/backbone-js-on-rails). Once you start getting in the groove of things the documentation is actually really good for quick look ups and you'll start to see that the framework is actually surprisingly tiny.
I also have a couple sample apps up on github that I used to learn backbone if you'd like to see some more examples: https://github.com/jwarzech/inspire_board https://github.com/jwarzech/realtime_hn
He has a framework on top of Backbone called Marionette that is well documented and makes handling large apps much easier: https://github.com/derickbailey/backbone.marionette
He then builds an email client app with it: https://github.com/derickbailey/bbclonemail
And has a ton of great articles on backbone: http://lostechies.com/derickbailey/category/backbone/
Definitely worth following his work.
Backbone expects you to have your architecture already in mind, and your use case to be more or less similar to DocumentCloud's. Then it will really shine.
The problem is that docs don't really define the whole picture. So you need to figure out stuff yourself, and may unexpectedly find yourself ‘fighting the framework’.
I personally think this is a problem. Backbone really should be re-positioned and turned into a full-blown client-side framework, with more control flow inversion; so that a developer would be able to just create ‘a Backbone project’, customize a few things and be ready to roll.
I'd tend to think that it's because Backbone is agnostic about your backend, and agnostic about your UI. That tends to leave a lot of the big pieces up to you...
That said, I like your premise. If you have more concrete thoughts about what "re-positioned and turned into a full-blown client-side framework, with more control flow inversion' ... would mean in practice, please open a ticket and describe your ideas for discussion.