resthub/resthub-backbone-stack — pull request #27 by bmeurant (2012-08-26 16:05:42+00:00)
Title: Manage pubsub handlers

This PR is related to #10.

Some important points have to be discussed related to this PR and issue #10.

1. **syntax and object keys problem**

Initially we wanted to **mimic Backbone events hash** in order to registrer automatically pubsub handers.

i.e., as Backbone do with events ...

Backbone.View.extend ({
events: {
"focusin ul.thumbnails li.thumbnail a":"elemFocused",
"focusout ul.thumbnails li.thumbnail a":"elemFocused",
"click li.thumbnail":"hideTooltips"
}
});

... implement pubsub handlers management:

Backbone.View.extend ({
events: {
"type1":"callback1",
"type2":"callback2",
"type3":"callback3"
}
});

This particular sample is OK but it seems to me an important feature **to be able to define globally (in view or even in app) some shared events, defined once and reused everywhere**. Something like :

App.Events.TYPE3 = "type3";

Backbone.View.extend({
events: {
"type1":"callback1",
"type2":"callback2",
// this is not valid !
TYPE3:"callback3"
}
});

But this is not possible in javascript (it is surely obvious to javascript experts but I did not realize that in a first approach) as well explained here : http://mathiasbynens.be/notes/javascript-properties.

So I had to find another syntax based on nested arrays :

Backbone.View.extend({
handles:[
["type1", "callback1"],
["type2", "callback2"],
[TYPE3, "callback3"],
["type4", function () {this.counts[5] += 1;}]
]
});

I preferred this first syntax but this one works ! @loicfrering: Is there another solution ?

2. **Inheritance strategy**

I implemented this Backbone.View extension by **extending directly ``Backbone.View``** (as commonly done in Backbone extensions) and not defining our own ``Backbone.View`` extension (e.g. ``Backbone.Resthub.View``).

Doing this:

- our extension is completely transparent in a user point of view : he only have to extend ``Backbone.View`` as he always done before.
- compatibility with others ``Backbone.View`` extension could be easier because they already extend ``Backbone.View`` and obviously not ``Backbone.Resthub.View``. But I'm not fully convinced of that because all extensions I saw before were inline ``View`` extensions (simply adding ``_.extend(Backbone.View.prototype, {``) that will be compatible anyway ...

But it necessitates some boilerplate to call handles parsing on View constructor while keeping ``View`` behaviour safe:

// Backbone.View extension
// -----------------------

var originalPrototype = Backbone.View.prototype;
var originalConstructor = Backbone.View;
var originalExtend = Backbone.View.extend;

Backbone.View = function () {
originalConstructor.apply(this, arguments);
this.subscribeHandles();
};

// Restore original prototype
Backbone.View.prototype = originalPrototype;
Backbone.View.extend = originalExtend;

Is this solution a good one ? Do we have other options (at least one : make a ``Resthub.View``) ? Is this the goal we want to achieve (in architectural terms) ?

@sdeleuze, @loicfrering : I am not sure of what solution I prefer and I'd like to discuss this point together.