-
Notifications
You must be signed in to change notification settings - Fork 171
/
index.js
98 lines (78 loc) · 3.17 KB
/
index.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
var fs = require("fs");
// Common dependencies to get LayoutManager running.
var Backbone = require("backbone");
var _ = require("underscore");
// Using Cheerio instead of jQuery, because Cheerio emulates a loosely
// compatible API that we further augment to ensure unit tests pass. It is
// also much faster than jsdom.
var $ = require("cheerio");
// This is to avoid unwanted errors thrown when using
// `Backbone.View#setElement`.
$.prototype.unbind = $.prototype.off = function() { return this; };
// Set a basic trim function to allow trimming whitespace from a template
$.trim = function(str) { return str.trim(); };
// Since jQuery is not being used and LayoutManager depends on a Promise
// implementation close to jQuery, we use `underscore.deferred` here which
// matches jQuery's Deferred API exactly. This is mixed into Cheerio to make
// it more seamless.
_.extend($, require("underscore.deferred"));
// Get Backbone and _ into the global scope.
_.defaults(global, { Backbone: Backbone, _: _ });
// Set the Backbone DOM library to be Cheerio.
Backbone.$ = $;
// Include the LayoutManager source, without eval.
require("../backbone.layoutmanager");
// Configure LayoutManager with some very useful defaults for Node.js
// environments. This allows the end user to simply consume instead of
// fighting with the desirable configuration.
Backbone.Layout.configure({
// Sensible default for Node.js is to load templates from the filesystem.
// This is similar to how we default to script tags in browser-land.
fetchTemplate: function(template) {
// Automatically add the `.html` extension.
template = template + ".html";
// Put this fetch into `async` mode to work better in the Node environment.
var done = this.async();
// By default read in the file from the filesystem relative to the code
// being executed.
fs.readFile(template, function(err, contents) {
// Ensure the contents are a String.
contents = String(contents);
// Any errors should be reported.
if (err) {
console.error("Unable to load file " + template + " : " + err);
return done(null);
}
// Pass the template contents back up.
done(_.template(contents));
});
},
// This is really the only way you will want to partially apply a view into
// a layout. Its entirely possible you'll want to do it differently, so
// this method is available to change.
partial: function($root, $el, rentManager, manager) {
var $filtered;
// If selector is specified, attempt to find it.
if (manager.selector) {
if (rentManager.noel) {
$filtered = $root.filter(manager.selector);
$root = $filtered.length ? $filtered : $root.find(manager.selector);
} else {
$root = $root.find(manager.selector);
}
}
// If no root found, return false.
if (!$root.length) {
return false;
}
// Use the insert method if the parent's `insert` argument is true.
if (rentManager.insert) {
this.insert($root, $el, rentManager.insertAt);
} else {
this.html($root, $el);
}
// If successfully added, return true.
return true;
}
});
module.exports = Backbone.Layout;