-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.view.manager.js
More file actions
77 lines (62 loc) · 2.48 KB
/
backbone.view.manager.js
File metadata and controls
77 lines (62 loc) · 2.48 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
// ==========================================================
// Close() prototype method
// ==========================================================
Backbone.View.prototype.close = function() {
this.remove();
this.unbind();
}
// ==========================================================
// Name() prototype method
// ==========================================================
Backbone.View.prototype.name = function() {
this.class = this.className ? this.className : "view";
this.name = this.class + "-" + this.cid;
return this.name;
}
// ==========================================================
// Use Mustache style {{ variables }}
// ==========================================================
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
// ==========================================================
// ==========================================================
// Backbone View Manager
// ==========================================================
// ==========================================================
Backbone.ViewManager = {
// Views that are currently visible
Visible : [],
// The ViewManager Add() method requires a view to be passed
// but also accepts an optional params object
Add : function(view, params) {
// Setup the target with the passed selector or assign it to the body
var target = params && params.selector ? params.selector : "body";
// If there is a current view, and you didn't pass keepView to keep the current view
if (this.currentView && !params.keepView){
this.currentView.close();
}
// Make the passed view the current view, then render the current view
this.currentView = view;
this.currentView.render();
// Add this view to the Visible array
this.Visible.push(view);
// Use the passed action if there is one
if ( !params || params && !params.action ) $(target).html(this.currentView.el);
else if ( params && params.action == "append" ) $(target).append(this.currentView.el);
// if there is a passed callback, call it now
if ( params && params.callback ) params.callback();
// Return the view back to where it was called
return this.currentView;
},
// The ViewManager Remove() method requires a view to be passed
Remove : function(view, close) {
// Loop through the views, and remove the view from the Visible array
for ( var i = 0; i < this.Visible.length; i++ ) {
if ( view.name == this.Visible[i].name ) {
this.Visible.splice(i,1);
if ( close ) view.close();
}
};
}
}