Give Your Apps a Backbone(.js)
joezimjs.com
joezimjs.com
To clarify, these are some of what I'm referring to (apologies for the code-style formatting - just trying to produce a list):
- Backbone.Model/YUI App Model:
- Backbone's .previousAttributes vs YUI's .lastChange, used to get the
previous value of a changed attribute since the last "change" event
- A separate, abstracted Model sync layer for communicating with a server
(side note: Backbone 0.9's "sync" event is being introduced in YUI in the
next release: http://stage.yuilibrary.com/yui/docs/model/index.html)
- .cid (Backbone) vs .clientId (YUI), auto-generated id guaranteed to be
unique among all models on the current page.
- configuring how the .id property is populated by overriding the
.idAttribute property
- .collection (Backbone: the Collection containing this model) vs .lists
(YUI: array of ModelLists containing this model)
- Backbone.View/YUI App View:
- .events object literal property that specifies event bindings via dom
event delegation, with the css selector in the key
- .el (Backbone) vs .container (YUI): managed property representing the
view's "container". All delegated events are bound to this element.
- Backbone.Collection/YUI App ModelList:
- .model specifying the underlying model type by referencing the type's
constructor function in the prototype definition directly (I've seen
it referenced by global name before, never by constructor reference
as Backbone does it)
- .getByCid() (Backbone) and getByClientId() (YUI): get a model out of a
collection via its auto-generated client id
- Backbone.Router/YUI App Controller: class that maps URL changes to methods
on the instance. They're also renaming "Controller" to "Router" in an
upcoming release, following Backbone's doing of the same in 0.5.3.
These are possibly too small to refer to as "paradigms", but the point still stands: they're borrowing these directly from Backbone. If they existed prior to Backbone, my apologies.Also, this is not necessarily a bad thing.
I have no experience with either option so an in-depth comparison would be tremendously valuable to me.
Not necessarily disagreeing, I've never used YUI3, but I'd love to hear more about it.
I've recently been dabbling in Backbone and, frankly, I'm struggling with lack of documentation. A better documented "competitor" in this space would likely win me as a user.
I was hoping it would be Ember/Amber, or something to that extent, but that doesn't appear to be significantly better documented either.
What do you consider YUI's strengths / weaknesses vs. Backbone? (Please say documentation).
I've found the documentation to be a great reference, but invariably, due to lack of examples, if I'm trying to implement something I haven't used before, I have to scour the internet for other information before I can generally make sense of the official backbone docs.
The best documentation I've found thus far has been other people's projects in Github. While trying not to cargo-cult copy/paste, seeing somebody else implement a feature similar to what I'm trying to do has been the best for me.
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.
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)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 });
}
});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.
The thing that I wish the project had was some official opinionated best practices guidelines. How to structure a growing app, build/minify workflow, pitfalls, etc. (Note: this wish may be due to my utter noobishness and I'm mostly monitoring the mailing list and reading all the questions to learn)
Other than that, I wish AMD was going to be supported in core, but tbranyen's requirejs use (shim) plugin is going pretty well.
Backbone is an MVC framework only, YUI is a lot more. Each tries to accomplish something very different.