-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackboneDo-0.5.js
185 lines (138 loc) · 4.93 KB
/
backboneDo-0.5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Backbone.CQRS.js
// (c) 2012 Jan Mühlemann
// Backbone.CQRS may be freely distributed under the MIT license.
(function(){
// Initial Setup
// -------------
// Save a reference to the global object.
var root = this;
// Save the value of the `Backbone` variable. All extended modules will
// be appended to Backbone namespace
var Backbone = root.Backbone;
Backbone.Do = {};
// Shortcut to underscore
var _ = root._;
// For Backbone's purposes, jQuery or Zepto owns the `$` variable.
var $ = root.jQuery || root.Zepto;
var noop = $.noop;
// Message Objects
// ---------------
Backbone.Do.Message = Backbone.Model.extend({
url: noop,
fetch: noop,
save: noop,
destroy: noop
});
// Response = Done
var Res = Backbone.Do.Message.extend({});
// Request = Do
Backbone.Do.Req = Backbone.CQRS.Message.extend({
emit: function() {
Backbone.Do.hub.emit(Backbone.Do.hub.reqChannel, this.parse(this.toJSON()));
},
parse: function(data) {
return data;
},
observe: function(callback) {
Backbone.Do.responseHandler.observe(this.id, callback);
}
});
// Event Handling
// --------------
// Hub will listen to responses
var hub = Backbone.CQRS.hub = {
reqChannel: 'do',
defaults: {
reqChannel: 'do',
resChannel: 'done',
responseId: 'reqId'
},
init: function(options) {
var self = this;
if (!this.initialized) {
this.initialized = true;
options = _.extend(this.defaults, options);
if (options.getRequestId) this.getRequestId = options.getRequestId;
if (options.parseResponse) this.parseResponse = options.parseResponse;
this.reqChannel = options.reqChannel;
// forward incoming events to eventHandler by emitting it to
// dispatchEvent -> eventHandler is bound to this 'channel'
this.on(options.resChannel, function(msg) {
// create an res object and set parsed message attributes
var res = new Response();
res.set(this.parseResponse(msg));
var attrs = res.toJSON();
res.reqId = self.getRequestId(attrs, options.responseId);
// emit it -> forward to eventHandler
this.emit('resolveResponse', res);
});
}
},
parseResponse: function(msg) {
var evt = msg;
if (typeof evt == 'string') {
evt = JSON.parse(evt);
}
return evt;
},
getRequestId: function(data, field) {
return dive(data, field);
}
};
_.extend(hub, Backbone.Events);
// we use Backbone.Event but provide EventEmitters interface
hub.on = hub.bind;
hub.emit = hub.trigger;
// Global Handler
// -------------------
Backbone.Do.ResponseHandler = function() {
this.initialize.apply(this, arguments);
};
_.extend(Backbone.Do.ResponseHandler.prototype, Backbone.Events, {
initialize: function() {
this.observedRequests = [];
// subscribe on incoming events
Backbone.Do.hub.on('resolveResponse', function(res) {
this.handle(res);
}, this);
},
// handles incoming events from hub
handle: function(res) {
// to observing commands
var pending = this.getPendingResponse(res);
if (pending) {
pending.callback(res);
this.removePendingResponse(res);
}
},
// add command to observe
// in the handle function an event matching the command will
// call the provided callback
observe: function(reqId, callback) {
this.observedRequests.push({id: reqId, callback: callback});
},
getPendingRequest: function(res) {
return _.detect(this.observedRequests, function(pend) {
return pend.id == res.reqId;
});
},
removePendingCommand: function(pending) {
var index = _.indexOf(this.observedRequests, pending);
this.observedRequests.splice(index, 1);
}
});
// create one instace of this global handler
Backbone.Do.responseHandler = new ResponseHandler();
// Functions
// ---------
var dive = function(obj, key) {
var keys = key.split('.');
var x = 0;
var value = obj;
while (keys[x]) {
value = value && value[keys[x]];
x++;
}
return value;
};
}).call(this);