/
app.js
97 lines (75 loc) · 2.23 KB
/
app.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
define([
// Libraries.
"jquery",
"lodash",
"backbone",
// Plugins.
"plugins/backbone.layoutmanager"
],
function($, _, Backbone) {
// Patch collection fetching to emit a `fetch` event.
Backbone.Collection.prototype.fetch = function() {
var fetch = Backbone.Collection.prototype.fetch;
return function() {
this.trigger("fetch");
return fetch.apply(this, arguments);
};
}();
// Provide a global location to place configuration settings and module
// creation.
var app = {
// The root path to run the application.
root: "/html/apps/movie-list-backbone/"
};
// Localize or create a new JavaScript Template object.
var JST = window.JST = window.JST || {};
// Configure LayoutManager with Backbone Boilerplate defaults.
Backbone.LayoutManager.configure({
manage: true,
paths: {
layout: "app/templates/",
template: "app/templates/"
},
fetch: function(path) {
path = path + ".html";
if (!JST[path]) {
$.ajax({ url: app.root + path, async: false }).then(function(contents) {
JST[path] = _.template(contents);
});
}
return JST[path];
}
});
// Mix Backbone.Events, modules, and layout management into the app object.
return _.extend(app, {
// Create a custom object with a nested Views object.
module: function(additionalProps) {
return _.extend({ Views: {} }, additionalProps);
},
// Helper for using layouts.
useLayout: function(name) {
// If already using this Layout, then don't re-inject into the DOM.
if (this.layout && this.layout.options.template === name) {
return this.layout;
}
// If a layout already exists, remove it from the DOM.
if (this.layout) {
this.layout.remove();
}
// Create a new Layout.
var layout = new Backbone.Layout({
template: name,
className: "layout " + name,
id: "layout"
});
// Insert into the DOM.
$("#main").empty().append(layout.el);
// Render the layout.
layout.render();
// Cache the refererence.
this.layout = layout;
// Return the reference, for chainability.
return layout;
}
}, Backbone.Events);
});