/
main.js
103 lines (81 loc) · 2.94 KB
/
main.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
require([
"namespace",
// Libs
"jquery",
"use!backbone",
// Modules
"modules/email"
],
function(epp, jQuery, Backbone, Email) {
var app = epp.app;
// Defining the application router, you can attach sub routers here.
var Router = Backbone.Router.extend({
routes: {
"": "index",
"compose": "compose"
},
index: function() {
var emails = new Email.Collection();
window.emails = emails;
emails.fetch().success(function(){
var main = new Backbone.LayoutManager({
template: 'main',
views: {
'#sidebar': new Email.Views.Sidebar()
}
});
var nav = main.view('#mainnav', new Email.Views.Nav(), true);
emails.each(function(email) {
main.views['#sidebar'].view("ul", new Email.Views.SidebarItem({ model: email}), true);
});
app.bind('showbody', function( model){
var reader = main.view('#reader', new Email.Views.Reader({ model: model }));
reader.render();
});
app.bind('showwriter', function( model){
var writer = main.view('#reader', new Email.Views.Writer({ model: model }));
writer.render();
});
main.render(function( el ){
$('#main').html( el );
})
});
},
compose: function() {
epp.app.router.index();
var writer = new Email.Views.Writer();
writer.render();
}
});
// Treat the jQuery ready function as the entry point to the application.
// Inside this function, kick-off all initialization, everything up to this
// point should be definitions.
jQuery(function($) {
// Shorthand the application namespace
var app = epp.app;
// Define your master router on the application namespace and trigger all
// navigation from this instance.
app.router = new Router();
// Trigger the initial route and enable HTML5 History API support
Backbone.history.start({ pushState: true });
});
// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a data-bypass
// attribute, bypass the delegation completely.
$(document).on("click", "a:not([data-bypass])", function(evt) {
// Get the anchor href and protcol
var href = $(this).attr("href");
var protocol = this.protocol + "//";
// Ensure the protocol is not part of URL, meaning its relative.
if (href && href.slice(0, protocol.length) !== protocol) {
// Stop the default event to ensure the link will not cause a page
// refresh.
evt.preventDefault();
// This uses the default router defined above, and not any routers
// that may be placed in modules. To have this work globally (at the
// cost of losing all route events) you can change the following line
// to: Backbone.history.navigate(href, true);
app.router.navigate(href, true);
}
});
});