Hacker News new | ask | show | jobs
by kjbekkelund 4841 days ago
Jeremy, what I've seen most people struggle with (including myself in the beginning) is those first couple of hundred lines of code. How does everything fit together? How should I handle templates? How to I create nested models? How do I stop thinking "in jQuery" and start thinking "in Backbone"?

I guess the thing is that the current documentation works really well when you understand Backbone. Before you understand it, getting started is really difficult. That's why we see so many getting started guides all around — the problem of course being that most of them are barely just ok. Backbone should have a really, really good getting started guide, which (I think) should be more opinionated than Backbone itself — i.e. it shows one (really good) way of doing things (that works well when you scale up your application). Or at least link to one.

I've heard many say "look at the todo example", but I think that it's so far away from a real app that it's difficult to use as a starting point. It doesn't use ajax, it relies heavily on global state, it puts templates in the HTML, lots of global `$` stuff, no routing, etcetc. Ok-ish choices, but not a great example for those getting started. E.g. having templates in the DOM is ok when you have 5 views. When you have more it's problematic, especially for testing and for precompiling templates.

Backbone is great when you grok it, but it takes a lot of time to find your own patterns that works really well. (Marionette and Chaplin are awesome, but still, first when you have some basic understanding of Backbone itself)

1 comments

>> How does everything fit together?

initialize models and controllers, then pass that data to newly initialized views. at any scale, this hardly differs.

>> How should I handle templates?

backbone is template agnostic so the answer is "however you'd like". because I use r.js, my preference is loading the templates as separate text modules and defining them on my view. when our production script is build, the templates are inlined into the javascript through r.js.

your qualms with the todo example (no AJAX, global variables, strange template handling) are things that are not at all pertinent to Backbone and are solved by other patterns. point being, Backbone has it's own model/view pattern, but you can look at it in isolation. your other concerns are things solved by patterns with no pertinent relation to Backbone.

I wasn't commenting on Backbone itself, I was commenting on getting started with Backbone. I've held 15+ workshops and presentations on Backbone and helped a lot of people getting started. These are the issues I have most often heard from them.
my point is that these questions seem either misdirected or simply answered. that is to say, it's not a framework. it's a set of classes. how you handle templates, how these classes fit together, etc is hardly a function of Backbone.

don't get me wrong. i've been there. i've had the same questions. but i think the answers i gave would have satisfied me when i was starting, had i had a mentor.

Catshirt, I actually would love to get feedback on this comment:

>> initialize models and controllers, then pass that data to newly initialized views. at any scale, this hardly differs.

Where do you find it makes the most sense to initialize models & controllers? I've done it in the router function but that feels really heavy and gets big.

Alternatively, since I use require.js I have used a view wrapper that initializes everything and then puts together the sub-views and models/collections as necessary. I like this approach because it feels more organized having it delegated out to my various view files.

Thanks in advance :)

router functions i think are sufficient. if size is a problem, multiple routers could help. different routers could also generalize instantiation for certain types of pages. if it gets more specific than that, i put it into the page view logic like you suggested. does this sound reasonable?

aside, but to my original point, you could use any router and you'd still have this problem. these type questions are probably best not answered by Backbone.

edit: no need to downvote. i am open to suggestions or criticism.