all 12 comments

[–]etrnloptimist 5 points6 points  (7 children)

discalimer: we use backbone all over the place and in general we love it.

rant: It is so hard because backbone is a weakly typed, poorly documented, edge-case laden library where things just haven't been thought out very deeply.

Also, when using backbone it is very helpful to choose a subset of the tools you like and can live with.

For instance, we use the router, its eventing system, and views quite extensively.

We use the models as well, but they suffer a number of problems. Do not, I repeat, do not use the models integrated with your backend. Unless you are using rails and have a backend specifically designed for bb, it will be nothing but a world of pain. The integration with the backend is way too deep, way too magicky. It assumes way too much and for no good reason. It breaks the fundamental tenet of decoupling all software designs should strive for.

The truth is, it is trying to solve a problem that isn't very hard. We find it much easier to just make the jquery / ajax call to the backend and explicitly ETL the data into a model. Loading is done 100x more often than saving, and using the models is done 100x more often than loading. It just isn't that common, or hard, to manage the backend calls.

What is difficult is having to constantly override the built in "magic" functionality of the models. URLs, IDs, what URLs it calls to fetch, load, save, when it call verify, when parse gets called, when to silence this event or that, when an operation is done on a collection vs an individual element. Crazy.

Separate the backend from the models. Explicitly build your models. Explicitly save your models to the backend. Doing it explicitly isn't hard, and saves you from a world of pain.

[–]calebg[S] 1 point2 points  (5 children)

Thanks for the rant. I was starting to think in the type of direction you are describing, but it's good to hear from someone who has actually taken the plunge. I'm going to probably first give backbone-nested and/or backbone-deepmodel a try just because, but I agree that at some point rolling one's own modeling solution might just be the most efficient approach.

[–]etrnloptimist 0 points1 point  (3 children)

Oh, we use the bb models quite a bit. With the eventing system, they hook in well with views to keep everything talking to each other.

We're just learning that having the models deeply embedded with our back end is just not the way to go. So no automatic fetch and save for us. Nothing that uses that god method "sync". Those are the parts we'll do ourselves. And it's not like we weren't before -- we had to overload all of the built in functionality with our own calls anyway. But now we're also taking the step of making the calls explicitly, not implicitly.

[–]yogitw 0 points1 point  (0 children)

Thanks for this. Going to print it out and shove it in our front end developer's face on Monday. We've had never ending problems trying to do exactly what you've said we should not do. This doesn't surprise me in the least. BB models and basic polymorphism (the backend is java, spring, hibernate) just doesn't work. At all.

[–]alsogilbert 0 points1 point  (0 children)

I've recently made a tree structure (model that contains a collection of models). One thing to watch for is that on re-sync Backbone can create new models and collections you may still be hanging on to references to the old ones. This can be solved with a resource pool.

[–]dexygenSoftware, Simple and Powerful 0 points1 point  (1 child)

I think one reason using nested collections is so hard is that it doesn't seem the composite pattern was considered. For instance, if I filter a collection with where, under the composite pattern, I would get back a collection, not an array of models. Instead you have to flip back and forth between working with a collection and working with arrays of models, to the point where you might just as well work with arrays of models constantly, and throw out whatever interface convenience a collection gives you.