Hacker News new | ask | show | jobs
by bct 5195 days ago
"Lack of guidance" is the way I'd describe it. The documentation is great, but it didn't help to steer me away from making architectural decisions that don't work well with Backbone when I was doing my first experiments.
2 comments

To follow up -- what sort of architectural decisions were those?
I'll try to think of examples and come back to it tomorrow.

For now: One thing that I'm still not clear on is when to wrap .get/.set.

Should I always write my own getters/setters and only use .get/.set internally? Right now I'm only wrapping them when the attributes need special processing, which leads to an inconsistent-feeling API.

I'd lean towards never, unless you have a case with really special semantics.

For run-of-the-mill attribute massaging, I'd just define a new method on the model specifically for that purpose. eg.

    book.loadFromAmazon(amazonJSON)
Hmmm, interesting. Here's a concrete example:

I've got an 'Organization' model and a 'Person' model. I want to use them interchangeably in some templates, so I need to be able to get their names in a consistent way.

An Organization just has a 'name', but a Person has a 'first_name' and a 'last_name'. I've been using a 'displayName' method to encapsulate the difference; would it make more sense to do something like:

    var Person = Backbone.Model.extend({
      initialize: function() {
        var name = this.get('first_name') + ' ' + this.get('last_name');
        this.set({ name: name }, { silent: true });
      }
    });
.displayName() seems like a perfect encapsulation IMO.
That is perhaps a better answer than the one I gave.

Without said guidance, I ended up created multiple views to represent the same area on the page instead of using subtemplates.

Same thing with Collections -- on my first try at something serious, I ended up with multiple collections to represent the same data set, but in different states.

Perhaps, for people who have been working extensively with Javascript before Backbone, this comes more intuitively, but for me, it's been a pretty painstaking process.

In addition to that, conflicting guidance from the various unofficial docs, without explanation of why they did something differently, led me to wondering which way made sense, and it generally wasn't until well after implementation that I realized I'd done it the wrong way.