forked from GeppettoJS/backbone.geppetto
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackbone.geppetto.js
More file actions
executable file
·216 lines (164 loc) · 6.04 KB
/
backbone.geppetto.js
File metadata and controls
executable file
·216 lines (164 loc) · 6.04 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Backbone.Geppetto v0.6.1
//
// Copyright (C) 2013 Model N, Inc.
// Distributed under the MIT License
//
// Documentation and full license available at:
// http://modeln.github.com/backbone.geppetto/
(function (factory) {
if (typeof define === "function" && define.amd) {
// Register as an AMD module if available...
define(["underscore", "backbone"], factory);
} else {
// Browser globals for the unenlightened...
factory(_, Backbone);
}
}(function(_, Backbone) {
"use strict";
if(!Backbone){
throw "Please include Backbone before Geppetto";
}
var Geppetto = {};
Geppetto.EVENT_CONTEXT_SHUTDOWN = "Geppetto:contextShutdown";
var contexts = {};
Geppetto.Context = function Context( options ) {
this.options = options || {};
this.parentContext = this.options.parentContext;
this.vent = {};
_.extend(this.vent, Backbone.Events);
if (_.isFunction(this.initialize)) {
this.initialize.apply(this, arguments);
}
this._contextId = _.uniqueId("Context");
contexts[this._contextId] = this;
this.mapCommands();
};
Geppetto.bindContext = function bindContext( options ) {
this.options = options || {};
var view = this.options.view;
var context = null;
if (typeof this.options.context === 'function') {
// create new context if we get constructor
context = new this.options.context(this.options);
// only close context if we are the owner
if (!view.close) {
view.close = function() {
view.trigger("close");
view.remove();
};
}
view.on("close", function() {
view.off("close");
context.unmapAll();
});
} else if (typeof this.options.context === 'object') {
// or use existing context if we get one
context = this.options.context;
}
view.context = context;
// map context events
_.each(view.contextEvents, function(callback, eventName) {
if (_.isFunction(callback)) {
context.listen(view, eventName, callback);
} else if (_.isString(callback)) {
context.listen(view, eventName, view[callback]);
}
});
return context;
};
Geppetto.Context.prototype.listen = function listen( target, eventName, callback ) {
if (arguments.length !== 3) {
throw "Expected 3 arguments (target, eventName, callback)";
}
if ( ! _.isObject(target) ||
! _.isFunction(target.listenTo) ||
! _.isFunction(target.stopListening)) {
throw "Target for listen() must define a 'listenTo' and 'stopListening' function";
}
if ( ! _.isString(eventName)) {
throw "eventName must be a String";
}
if ( ! _.isFunction(callback)) {
throw "callback must be a function";
}
return target.listenTo( this.vent, eventName, callback, target );
};
Geppetto.Context.prototype.dispatch = function dispatch( eventName, eventData ) {
if ( ! _.isUndefined(eventData) && ! _.isObject(eventData) ) {
throw "Event payload must be an object";
}
eventData = eventData || {};
eventData.eventName = eventName;
this.vent.trigger( eventName, eventData ); };
Geppetto.Context.prototype.dispatchToParent = function dispatchToParent( eventName, eventData ) {
if ( this.parentContext ) {
this.parentContext.vent.trigger( eventName, eventData );
}
};
Geppetto.Context.prototype.dispatchGlobally = function dispatchGlobally( eventName, eventData ) {
_.each( contexts, function ( context, contextId ) {
context.vent.trigger( eventName, eventData );
} );
};
Geppetto.Context.prototype.mapCommand = function mapCommand( eventName, CommandConstructor ) {
var _this = this;
this.vent.listenTo( this.vent, eventName, function ( eventData ) {
var commandInstance = new CommandConstructor();
commandInstance.context = _this;
commandInstance.eventName = eventName;
commandInstance.eventData = eventData;
if (_.isFunction(commandInstance.execute)) {
commandInstance.execute();
}
}, this );
};
Geppetto.Context.prototype.mapCommands = function mapCommands() {
var _this = this;
_.each(this.commands, function(mixedType, eventName) {
if(_.isArray(mixedType)){
_.each(mixedType, function(commandClass){
_this.mapCommand(eventName, commandClass);
});
}else{
_this.mapCommand(eventName, mixedType);
}
});
};
Geppetto.Context.prototype.unmapAll = function unmapAll() {
this.vent.stopListening();
delete contexts[this._contextId];
this.dispatchToParent(Geppetto.EVENT_CONTEXT_SHUTDOWN);
};
Geppetto.Context.extend = Backbone.View.extend;
var debug = {
contexts : contexts,
countEvents : function countEvents() {
var numEvents = 0;
_.each(contexts, function(context, id) {
if (contexts.hasOwnProperty(id)) {
numEvents += _.size(context.vent._events);
}
});
return numEvents;
},
countContexts: function countContexts() {
var numContexts = 0;
_.each(contexts, function(context, id) {
if (contexts.hasOwnProperty(id)){
numContexts++;
}
});
return numContexts;
}
};
Geppetto.setDebug = function setDebug( enableDebug ) {
if (enableDebug) {
this.debug = debug;
} else {
this.debug = undefined;
}
return this.debug;
};
Backbone.Geppetto = Geppetto;
return Geppetto;
}));