-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvm.js
43 lines (37 loc) · 1.13 KB
/
vm.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
// Define View Manager to manage view states
window.VM = window.VM || {};
// VM.views hold all references to existing views
VM.views = VM.views || {};
// Close existing view
VM.closeView = function(name) {
if ( typeof VM.views[name] !== 'undefined') {
// Cleanup view
// Remove all of the view's delegated events
VM.views[name].undelegateEvents();
// Remove view from the DOM
VM.views[name].remove();
// Removes all callbacks on view
VM.views[name].off();
if ( typeof VM.views[name].close === 'function') {
VM.views[name].close();
}
}
};
// VM.createView always cleans up existing view before
// creating a new one.
// callback function always return a new view instance
VM.createView = function(name, callback) {
VM.closeView(name);
VM.views[name] = callback();
return VM.views[name];
};
// VM.reuseView always returns existing view. Otherwise it
// execute callback function to return new view
// callback function always return a new view instance
VM.reuseView = function(name, callback) {
if ( typeof VM.views[name] !== 'undefined') {
return VM.views[name];
}
VM.views[name] = callback();
return VM.views[name];
};