Backbone.js 0.9.9 Released
backbonejs.org
backbonejs.org
Some of the more significant updates are:
* `listenTo` and `stopListening` for easier event unbinding.
* HTTP PATCH support, for partial updates.
* collection.update(models) for "smart" add/remove/merge in one method.
* Events.once, a-la jQuery. Also jQuery-style event map syntax.
* Lots of performance tuning for triggering millions of events per second
(should you find the need) in modern browsers.
http://jsperf.com/events-vs-events2/3This doesn't account for any child-views, or views that don't use .remove(), so you'll need to make sure those are taken care of when a view is destroyed. But in most cases, the garbage collection issues that have been seen relating to views & events are taken care of.
`listenTo` and `stopListening` just give you an inverted (and often easier) way to manage the references ... so that you can remove all of the references from the "other" side. For example:
view.listenTo(model, 'change', view.renderChange);
view.listenTo(collection, 'add', view.addItem);
view.listenTo(Backbone, 'initialLoad', view.reinit);
# ... and then, when you want to get rid of the view:
view.remove();
# Calls view.stopListening(), removing *all* of the
# events from "view" bound on all of those different objects.
Note that none of this is necessary if you throw away both sides of the bound event at the same time -- those will still be GC'd as usual. Also, this isn't just for Views. The methods are available on anything that mixes in Backbone.Events.> Tell an object to listen to a particular event on an other object. The advantage of using this form, intead of other.on(event, callback), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on.
I had to read this a few times and the given example to see that the convenience comes from tracking events on the listening object side.
Using "object" and "other" for the object variable names makes it a little confusing. I didn't realize the bolded "object" meant the listener object, as opposed to the object being listened to.
> listenTo object.listenTo(other, event, callback)
I think changing the naming to be more specific and updating the description names could help out.
listenTo observingObject.listenTo(actingObject, event, callback)
One caveat is that this breaks consistency with the other examples in this section (i.e. object.once(...) object.trigger(...)). 'Backbone' is undefined.Great release anyway the event handling is more than welcome and Backbone has been a joy to use for the past few months. Thank you for that.
And yes please for Events.once support.
should fix all those ghost view events right?
I was using it to check the raw response and determine if the collection had actually changed & then supress unnecessary view renders. I worked around it by just stringifying the response instead, but it was a breaking change for some of my code.
Otherwise I've been playing around and it looks cool so far.
I'm also of the mind that it ought to be a function that works purely in terms of your data. For your particular use case (call collection.fetch(), and don't re-render if nothing has changed) -- give the new collection.fetch({update: true}) a try. That should give you zero events if all of the model contents are identical to what's already in the collection.
This is great, up until now I've extended the app with Backbone.Event like:
App = {
view = {},
model = {},
collection = {},
events = {},
_.extend(App.events, Backbone.events)
Congrats on the soon-to-be 1.0 release :) define(["Backbone", "underscore"], function(Backbone, _) {
return _.extend({}, Backbone.Events);
});
and a dependency on 'events' in all our modules: define(["Backbone", "events"), function(Backbone, Events) {
// stuff
Events.on("event", stuff);
});typo "intead". ;)
Thanks for this great update. I like Backbone's Minimalist approach to structured JS programming. Love CS even more.
Will port my site to it right now.
The "event bus" and "listenTo" are very expected modifications I also liked the "Merge and Patch", I just got confused about the get using both CID and ID but I will take a look at the code later.
I can't figure why people at ThoughtWorks had put this beauty in the freezer, I would love to hear their reasons because that " abstraction pushed too far " doesn't do it for me.
this.registeredModelEvents.push({
model: model,
eventName: eventName,
callback: callback,
context: context
});
model.on(eventName, callback, context);
Here's to hoping we can update to 0.9.9 soon. Shouldn't be a problem."The Backbone object now extends Events so that you can use it as a global event bus, if you like."
would be great.
EventBus = _.extend({}, Backbone.Events);Although if Backbone keeps rolling in good features marionette's usefulness gets less and less ;)
Also (at least for me):-
Backbone learning curve: 1-2 months
AngularJS learning curve: 3-5 days
My backbone code is very easy to understand, organized and not a mess at all. It doesn't need to be for everyone, but I think your recommendation may be misplaced.
I've been writing MVC apps in JS and ECMA script in general (eg AS 3) for a good time now, and have used other MVCish frameworks before (Dojo, Flex etc). Backbone has a solid implementation of models, views and events. If you understand the fundamentals of Javascript (knowing event listener closures are references that'll prevent GC) and write modular code you can do great things with it without any "mess".
Having not tried any JS MVC framework before, I also have the problem of thinking backbone js doesn't seem to be so good as everybody is saying. Although I have to admit that I did not spend enough time yet. But from the examples (in particular the famous Todo example), I got the impression backbone js only works fine for apps with low complexity.
But who am I to judge, having it not used yet seriously...
There is literally a couple of days worth of reading to get up and running with either backbone or angular and the rest of the time is just standard write code -> question how good it is -> research better ways -> refactor loop that everyone should go through when picking up something new.
How steep you find a learning curve is likely to be influenced by your technical history.
That said, the difference in difficulty, in my experience, is certainly not an order of magnitude.
Angular offers a different and more indirect set of abstractions, which means that (for me) it's harder to reason about what's going on, and it's not really feasible to look at the Angular source for clues. Angular feels like a good approach if you're willing to adopt it fully and follow its conventions, but this surely takes longer to learn fully than Backbone does.
Also, Backbone plays incredibly well with CoffeeScript, if you like that kind of thing (I do, but I understand that not everyone does).
Ember - 10000000 years
Backbone gives you tools for modeling the client-side data of your JavaScript application, and having an interface that reacts dynamically to those changes. In Backbone, the emphasis is on the data and rich models; in Angular, the emphasis is on the HTML structure.
Fortunately, there are many good examples you can take a look at for both -- http://builtwith.angularjs.org vs. http://backbonejs.org/#examples. A longer answer to the question is available in the FAQ, here: http://backbonejs.org/#FAQ-why-backbone
That being said, I see no problem at all with using them both at the same time. There is a Knockback project, which I found after I wrote a hundred lines of code helper that does exactly the same thing, and I can tell that the work great together. Backbone is good at the Model layer, it's Model ad Collection classes are very good; Knockout on the other hand is exceptionally good when it comes to Views and Controllers, almost entirely eliminating the latter.
I think that people fear the complexity of using both frameworks in one app, but in my experience it's not that bad. Everyone should give it a try.