tableling-rails 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/lib/tableling-rails/version.rb +1 -1
- data/spec/dummy/app/assets/javascripts/application.js +6 -1
- data/spec/dummy/app/assets/javascripts/books.js +6 -4
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/log/development.log +5136 -4946
- data/spec/dummy/tmp/cache/assets/BF1/770/sprockets%2F8228a53c539c582499241527570aa216 +0 -0
- data/spec/dummy/tmp/cache/assets/C3F/0C0/sprockets%2F4643701467ac62c314510c8dd021916f +0 -0
- data/spec/dummy/tmp/cache/assets/C53/AD0/sprockets%2F712078516a81502575c831cb0a8e898e +0 -0
- data/spec/dummy/tmp/cache/assets/C59/0F0/sprockets%2F099f4e84a443568245ff0674030f6e05 +0 -0
- data/spec/dummy/tmp/cache/assets/C60/CF0/sprockets%2F32a45292ee67867d8715952f3072ad84 +0 -0
- data/spec/dummy/tmp/cache/assets/C6F/0E0/sprockets%2F82372776487d948ef7666d304d6fb345 +0 -0
- data/spec/dummy/tmp/cache/assets/C80/840/sprockets%2F562c2d168da585f80579347d10790a0a +0 -0
- data/spec/dummy/tmp/cache/assets/C82/F20/sprockets%2F117015d65237e3682f5b21c87d58ab95 +0 -0
- data/spec/dummy/tmp/cache/assets/C86/280/sprockets%2F1345c9947753f018f45371ec5083fa2d +0 -0
- data/spec/dummy/tmp/cache/assets/C91/350/sprockets%2F6864c49a10954375948d9f306db308fb +0 -0
- data/spec/dummy/tmp/cache/assets/C9A/900/sprockets%2Fc1b5b7611d7c10e124512724f29f241d +0 -0
- data/spec/dummy/tmp/cache/assets/CAF/540/sprockets%2F8f42e689b84f0b25c0180046467adf31 +0 -0
- data/spec/dummy/tmp/cache/assets/CB1/9A0/sprockets%2F57805dfa396248a9665cb4a0c85116d0 +0 -0
- data/spec/dummy/tmp/cache/assets/CC1/4E0/sprockets%2F6ef77414c793c84a437244d7d965ba90 +0 -0
- data/spec/dummy/tmp/cache/assets/CC3/B10/sprockets%2F0a2c59284c7a5d99581883a9763b5ad8 +0 -0
- data/spec/dummy/tmp/cache/assets/CC9/940/sprockets%2F8128c279185cf97dba7914278ce838b4 +0 -0
- data/spec/dummy/tmp/cache/assets/CCA/0C0/sprockets%2F6c3e053393afe303432ca73f1b41490b +0 -0
- data/spec/dummy/tmp/cache/assets/CD5/2C0/sprockets%2F166c056119ebdfb8b7104c97b424b423 +0 -0
- data/spec/dummy/tmp/cache/assets/CDB/610/sprockets%2F7adf20860b018477a95cdb8321b49d16 +0 -0
- data/spec/dummy/tmp/cache/assets/CDD/F50/sprockets%2F7353f31f80f1139aade47728a62af336 +0 -0
- data/spec/dummy/tmp/cache/assets/CE9/760/sprockets%2F5ae7fab601683996918936cad21fc244 +0 -0
- data/spec/dummy/tmp/cache/assets/CE9/B90/sprockets%2Fea2eb066933b1d8b5949972c3c79353a +0 -0
- data/spec/dummy/tmp/cache/assets/CF1/AF0/sprockets%2F14dc9633b61024231ebec0c1a4a0259f +0 -0
- data/spec/dummy/tmp/cache/assets/D02/340/sprockets%2Fae58ed66f72137a594b0912bab600e03 +0 -0
- data/spec/dummy/tmp/cache/assets/D0A/AC0/sprockets%2Fc154991d0a7564e15c0fb4a555c5c73d +0 -0
- data/spec/dummy/tmp/cache/assets/D0B/190/sprockets%2F68cb0d1054ca546fc473274c4ac8737c +0 -0
- data/spec/dummy/tmp/cache/assets/D11/530/sprockets%2F13bc8c66140adef2e97648630866aae3 +0 -0
- data/spec/dummy/tmp/cache/assets/D16/AA0/sprockets%2Ff181e659a47e2cf9c257f32d80452bd3 +0 -0
- data/spec/dummy/tmp/cache/assets/D18/840/sprockets%2F50c1885539f61359c48af74fcae1df31 +0 -0
- data/spec/dummy/tmp/cache/assets/D1B/940/sprockets%2F7c4819d1dd9b64a22941d8c7bd1e6954 +0 -0
- data/spec/dummy/tmp/cache/assets/D2B/1F0/sprockets%2F6a4c116e8316d082bb0ffae02e94b107 +0 -0
- data/spec/dummy/tmp/cache/assets/D3C/E20/sprockets%2Ffc6c44ec250bc532c55649e5292b5c1f +0 -0
- data/spec/dummy/tmp/cache/assets/D4B/C30/sprockets%2Fe8aa746e637d69ff0ac1e71e528c5292 +0 -0
- data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/spec/dummy/tmp/cache/assets/D5A/E20/sprockets%2Febf23d670481ba70b341aa2eb6e37b08 +0 -0
- data/spec/dummy/tmp/cache/assets/D5C/1F0/sprockets%2F2d3f2cfd98bf787168fb59f84ab24784 +0 -0
- data/spec/dummy/tmp/cache/assets/D5D/C60/sprockets%2Ffa0336b42d01aa9173d9d4c12fc4e82d +0 -0
- data/spec/dummy/tmp/cache/assets/D65/DD0/sprockets%2Fed4503679ef66f0e597bfc738bc9e698 +0 -0
- data/spec/dummy/tmp/cache/assets/D68/930/sprockets%2F5a15e52e84502f9fceb7ea04020f65fc +0 -0
- data/spec/dummy/tmp/cache/assets/D8C/140/sprockets%2F2afdbb3902a1be5d2f045cfc837903a3 +0 -0
- data/spec/dummy/tmp/cache/assets/D8E/660/sprockets%2F3a3eed926bdf64000ccacb36022f78d4 +0 -0
- data/spec/dummy/tmp/cache/assets/D95/690/sprockets%2Fe4d19a04705ee5d72c7fa64dc2070ddb +0 -0
- data/spec/dummy/tmp/cache/assets/DA4/EC0/sprockets%2F8da2289e16c1d0cac94aa825ee18d99d +0 -0
- data/spec/dummy/tmp/cache/assets/DA6/120/sprockets%2Fc5880aca76ccbb51f9388362e8afc1e6 +0 -0
- data/spec/dummy/tmp/cache/assets/DA7/390/sprockets%2F25e744cd1b6f8f7ce1d52c1e86a8f19a +0 -0
- data/spec/dummy/tmp/cache/assets/DAB/3E0/sprockets%2F63d2fa621beec4fe878f9eb2884426ab +0 -0
- data/spec/dummy/tmp/cache/assets/DAB/660/sprockets%2F9a8c36769afcc7027b4b6ee0c3e77ce5 +0 -0
- data/spec/dummy/tmp/cache/assets/DBF/EB0/sprockets%2F2afcec323c8ba12a2b7c852f23aa589e +0 -0
- data/spec/dummy/tmp/cache/assets/DCE/9F0/sprockets%2F8fa8c08ea8da97c06cd6c32fe3e613b0 +0 -0
- data/spec/dummy/tmp/cache/assets/DCF/E50/sprockets%2Fc39ff2ed4e1fb4269c39d415acf0d90b +0 -0
- data/spec/dummy/tmp/cache/assets/DD8/0A0/sprockets%2Fb2c3097effcd6084ec75e1f745d1d7dd +0 -0
- data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/spec/dummy/tmp/cache/assets/DF0/040/sprockets%2F04bbbe1ee5ca914173cd90cf6e8a4e0b +0 -0
- data/spec/dummy/tmp/cache/assets/DF7/C80/sprockets%2Ff503b3666caf04beb7cfe54cbdb0575e +0 -0
- data/spec/dummy/tmp/cache/assets/DFA/B70/sprockets%2F262fb95d3f7cfc16febb0f0128ac38ce +0 -0
- data/spec/dummy/tmp/cache/assets/E03/260/sprockets%2Fd1fcfea59ff53c32557ffcd93016cb2e +0 -0
- data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/cache/assets/E0A/510/sprockets%2F8efee416ba436e75bea85d4d367faea7 +0 -0
- data/spec/dummy/tmp/cache/assets/E43/810/sprockets%2F79b0ef75ceb26a9c8fca797db3efba96 +0 -0
- data/spec/dummy/tmp/cache/assets/E96/1B0/sprockets%2F6d5fe51f9c1a3d2f898becd0bcddef3e +0 -0
- data/spec/dummy/tmp/cache/assets/ED4/410/sprockets%2F082adbb8e2abd8a21c8b34aebbbfbfad +0 -0
- data/spec/dummy/tmp/pids/server.pid +1 -0
- data/vendor/assets/javascripts/tableling.js +3 -543
- data/vendor/assets/javascripts/tableling/bootstrap.js +230 -0
- data/vendor/assets/javascripts/tableling/core.js +321 -0
- metadata +11 -99
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +0 -78
- data/spec/dummy/tmp/cache/assets/C20/C20/sprockets%2F932e86665765e61017be1015b249d270 +0 -0
- data/spec/dummy/tmp/cache/assets/C98/B70/sprockets%2F64db420a3a172209bf0b734312a4888a +0 -0
- data/spec/dummy/tmp/cache/assets/CC1/B50/sprockets%2F13559b77aaaf524896e427719ef5d354 +0 -0
- data/spec/dummy/tmp/cache/assets/CD4/420/sprockets%2F19b3d492932dc6a59093dfb1aa852101 +0 -0
- data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/spec/dummy/tmp/cache/assets/CD8/520/sprockets%2F89128ce416ae74c4148c5c1f281e111c +0 -0
- data/spec/dummy/tmp/cache/assets/CDA/600/sprockets%2F98ab7e75433ba5d503533fb00b488c80 +0 -0
- data/spec/dummy/tmp/cache/assets/CE1/0F0/sprockets%2F92d30845fe3923e231cfe7e056a654b4 +0 -0
- data/spec/dummy/tmp/cache/assets/CE2/E70/sprockets%2Fd569808c7b411447dec0137e4d1ee707 +0 -0
- data/spec/dummy/tmp/cache/assets/D0A/780/sprockets%2F3d727e58c55c30009bbaf1462e466d4d +0 -0
- data/spec/dummy/tmp/cache/assets/D21/890/sprockets%2F73f9cd97cac4382d565e7b835709a2e3 +0 -0
- data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/D3A/720/sprockets%2F0e2516c1cac22716289bfc87d5ea9a26 +0 -0
- data/spec/dummy/tmp/cache/assets/D4B/BF0/sprockets%2Fdf124de0072e308f8e157eae77b85d88 +0 -0
- data/spec/dummy/tmp/cache/assets/D54/910/sprockets%2F787b6a4c2386b5edf98f90508b85cc3d +0 -0
- data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/spec/dummy/tmp/cache/assets/D63/170/sprockets%2Fb5b73c3b0862ee59b701a6819b2aa4cd +0 -0
- data/spec/dummy/tmp/cache/assets/D66/580/sprockets%2Ffe6bb9bb5ef9c54f501650b1365c1b05 +0 -0
- data/spec/dummy/tmp/cache/assets/D6A/F40/sprockets%2F53fdbea64f1803eebb72f426f74110d8 +0 -0
- data/spec/dummy/tmp/cache/assets/D6B/D30/sprockets%2Faeb25572f539ffdbb122e9de1218572a +0 -0
- data/spec/dummy/tmp/cache/assets/D6F/110/sprockets%2F2c8c9177efb7e5be1a7055bb0d44792a +0 -0
- data/spec/dummy/tmp/cache/assets/D76/3F0/sprockets%2Fbb569c97f9cc01af670906b40c6be9e2 +0 -0
- data/spec/dummy/tmp/cache/assets/D81/510/sprockets%2F3e6bd5a2d6d0d88f61f5b616f7d879b5 +0 -0
- data/spec/dummy/tmp/cache/assets/D82/940/sprockets%2F29cd0ae31066baa6f2cb92b5c6305aa2 +0 -0
- data/spec/dummy/tmp/cache/assets/D85/D20/sprockets%2F7f7f398c298ca8a1e812b6c9832bcf6e +0 -0
- data/spec/dummy/tmp/cache/assets/D97/230/sprockets%2F0f46cb65e018de57d2c8b480a0ec5dd3 +0 -0
- data/spec/dummy/tmp/cache/assets/DA1/F00/sprockets%2F7b23e35dd61e9d734a59179e2d2aebea +0 -0
- data/spec/dummy/tmp/cache/assets/DA3/950/sprockets%2F38737f6fa54dbda0dfe3ce4c51710e79 +0 -0
- data/spec/dummy/tmp/cache/assets/DB4/7A0/sprockets%2F900105aaabba7938ce23f1e0aade71e5 +0 -0
- data/spec/dummy/tmp/cache/assets/DB4/F90/sprockets%2Fcf5e68e329cedb58b84f445f2b8e37b5 +0 -0
- data/spec/dummy/tmp/cache/assets/DE2/6D0/sprockets%2Fdcaa7d2b2814413aa75d3ba6ada3c6b5 +0 -0
- data/spec/dummy/tmp/cache/assets/DF0/D60/sprockets%2F5a54a1ecc1db271edfc0727bfcb8d912 +0 -0
- data/spec/dummy/tmp/cache/assets/DFE/E10/sprockets%2F9a19dfdd7eb0bf5262919a0207ecfdbf +0 -0
- data/spec/dummy/tmp/cache/assets/E33/8F0/sprockets%2F4933e07f8ef2da7eb5bee9a6fcac3d41 +0 -0
- data/spec/dummy/tmp/cache/assets/E67/730/sprockets%2Fadd4efdfa7661cc60e8ea63cef1f0f98 +0 -0
- data/vendor/assets/javascripts/tableling.backbone.js +0 -3638
- data/vendor/assets/javascripts/tableling.world.js +0 -14768
Binary file
|
Binary file
|
Binary file
|
@@ -1,3638 +0,0 @@
|
|
1
|
-
// Backbone.js 0.9.2
|
2
|
-
|
3
|
-
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
4
|
-
// Backbone may be freely distributed under the MIT license.
|
5
|
-
// For all details and documentation:
|
6
|
-
// http://backbonejs.org
|
7
|
-
|
8
|
-
(function(){
|
9
|
-
|
10
|
-
// Initial Setup
|
11
|
-
// -------------
|
12
|
-
|
13
|
-
// Save a reference to the global object (`window` in the browser, `global`
|
14
|
-
// on the server).
|
15
|
-
var root = this;
|
16
|
-
|
17
|
-
// Save the previous value of the `Backbone` variable, so that it can be
|
18
|
-
// restored later on, if `noConflict` is used.
|
19
|
-
var previousBackbone = root.Backbone;
|
20
|
-
|
21
|
-
// Create a local reference to slice/splice.
|
22
|
-
var slice = Array.prototype.slice;
|
23
|
-
var splice = Array.prototype.splice;
|
24
|
-
|
25
|
-
// The top-level namespace. All public Backbone classes and modules will
|
26
|
-
// be attached to this. Exported for both CommonJS and the browser.
|
27
|
-
var Backbone;
|
28
|
-
if (typeof exports !== 'undefined') {
|
29
|
-
Backbone = exports;
|
30
|
-
} else {
|
31
|
-
Backbone = root.Backbone = {};
|
32
|
-
}
|
33
|
-
|
34
|
-
// Current version of the library. Keep in sync with `package.json`.
|
35
|
-
Backbone.VERSION = '0.9.2';
|
36
|
-
|
37
|
-
// Require Underscore, if we're on the server, and it's not already present.
|
38
|
-
var _ = root._;
|
39
|
-
if (!_ && (typeof require !== 'undefined')) _ = require('underscore');
|
40
|
-
|
41
|
-
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
|
42
|
-
var $ = root.jQuery || root.Zepto || root.ender;
|
43
|
-
|
44
|
-
// Set the JavaScript library that will be used for DOM manipulation and
|
45
|
-
// Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery,
|
46
|
-
// Zepto, or Ender; but the `setDomLibrary()` method lets you inject an
|
47
|
-
// alternate JavaScript library (or a mock library for testing your views
|
48
|
-
// outside of a browser).
|
49
|
-
Backbone.setDomLibrary = function(lib) {
|
50
|
-
$ = lib;
|
51
|
-
};
|
52
|
-
|
53
|
-
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
|
54
|
-
// to its previous owner. Returns a reference to this Backbone object.
|
55
|
-
Backbone.noConflict = function() {
|
56
|
-
root.Backbone = previousBackbone;
|
57
|
-
return this;
|
58
|
-
};
|
59
|
-
|
60
|
-
// Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option
|
61
|
-
// will fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and
|
62
|
-
// set a `X-Http-Method-Override` header.
|
63
|
-
Backbone.emulateHTTP = false;
|
64
|
-
|
65
|
-
// Turn on `emulateJSON` to support legacy servers that can't deal with direct
|
66
|
-
// `application/json` requests ... will encode the body as
|
67
|
-
// `application/x-www-form-urlencoded` instead and will send the model in a
|
68
|
-
// form param named `model`.
|
69
|
-
Backbone.emulateJSON = false;
|
70
|
-
|
71
|
-
// Backbone.Events
|
72
|
-
// -----------------
|
73
|
-
|
74
|
-
// Regular expression used to split event strings
|
75
|
-
var eventSplitter = /\s+/;
|
76
|
-
|
77
|
-
// A module that can be mixed in to *any object* in order to provide it with
|
78
|
-
// custom events. You may bind with `on` or remove with `off` callback functions
|
79
|
-
// to an event; trigger`-ing an event fires all callbacks in succession.
|
80
|
-
//
|
81
|
-
// var object = {};
|
82
|
-
// _.extend(object, Backbone.Events);
|
83
|
-
// object.on('expand', function(){ alert('expanded'); });
|
84
|
-
// object.trigger('expand');
|
85
|
-
//
|
86
|
-
var Events = Backbone.Events = {
|
87
|
-
|
88
|
-
// Bind one or more space separated events, `events`, to a `callback`
|
89
|
-
// function. Passing `"all"` will bind the callback to all events fired.
|
90
|
-
on: function(events, callback, context) {
|
91
|
-
|
92
|
-
var calls, event, node, tail, list;
|
93
|
-
if (!callback) return this;
|
94
|
-
events = events.split(eventSplitter);
|
95
|
-
calls = this._callbacks || (this._callbacks = {});
|
96
|
-
|
97
|
-
// Create an immutable callback list, allowing traversal during
|
98
|
-
// modification. The tail is an empty object that will always be used
|
99
|
-
// as the next node.
|
100
|
-
while (event = events.shift()) {
|
101
|
-
list = calls[event];
|
102
|
-
node = list ? list.tail : {};
|
103
|
-
node.next = tail = {};
|
104
|
-
node.context = context;
|
105
|
-
node.callback = callback;
|
106
|
-
calls[event] = {tail: tail, next: list ? list.next : node};
|
107
|
-
}
|
108
|
-
|
109
|
-
return this;
|
110
|
-
},
|
111
|
-
|
112
|
-
// Remove one or many callbacks. If `context` is null, removes all callbacks
|
113
|
-
// with that function. If `callback` is null, removes all callbacks for the
|
114
|
-
// event. If `events` is null, removes all bound callbacks for all events.
|
115
|
-
off: function(events, callback, context) {
|
116
|
-
var event, calls, node, tail, cb, ctx;
|
117
|
-
|
118
|
-
// No events, or removing *all* events.
|
119
|
-
if (!(calls = this._callbacks)) return;
|
120
|
-
if (!(events || callback || context)) {
|
121
|
-
delete this._callbacks;
|
122
|
-
return this;
|
123
|
-
}
|
124
|
-
|
125
|
-
// Loop through the listed events and contexts, splicing them out of the
|
126
|
-
// linked list of callbacks if appropriate.
|
127
|
-
events = events ? events.split(eventSplitter) : _.keys(calls);
|
128
|
-
while (event = events.shift()) {
|
129
|
-
node = calls[event];
|
130
|
-
delete calls[event];
|
131
|
-
if (!node || !(callback || context)) continue;
|
132
|
-
// Create a new list, omitting the indicated callbacks.
|
133
|
-
tail = node.tail;
|
134
|
-
while ((node = node.next) !== tail) {
|
135
|
-
cb = node.callback;
|
136
|
-
ctx = node.context;
|
137
|
-
if ((callback && cb !== callback) || (context && ctx !== context)) {
|
138
|
-
this.on(event, cb, ctx);
|
139
|
-
}
|
140
|
-
}
|
141
|
-
}
|
142
|
-
|
143
|
-
return this;
|
144
|
-
},
|
145
|
-
|
146
|
-
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
147
|
-
// passed the same arguments as `trigger` is, apart from the event name
|
148
|
-
// (unless you're listening on `"all"`, which will cause your callback to
|
149
|
-
// receive the true name of the event as the first argument).
|
150
|
-
trigger: function(events) {
|
151
|
-
var event, node, calls, tail, args, all, rest;
|
152
|
-
if (!(calls = this._callbacks)) return this;
|
153
|
-
all = calls.all;
|
154
|
-
events = events.split(eventSplitter);
|
155
|
-
rest = slice.call(arguments, 1);
|
156
|
-
|
157
|
-
// For each event, walk through the linked list of callbacks twice,
|
158
|
-
// first to trigger the event, then to trigger any `"all"` callbacks.
|
159
|
-
while (event = events.shift()) {
|
160
|
-
if (node = calls[event]) {
|
161
|
-
tail = node.tail;
|
162
|
-
while ((node = node.next) !== tail) {
|
163
|
-
node.callback.apply(node.context || this, rest);
|
164
|
-
}
|
165
|
-
}
|
166
|
-
if (node = all) {
|
167
|
-
tail = node.tail;
|
168
|
-
args = [event].concat(rest);
|
169
|
-
while ((node = node.next) !== tail) {
|
170
|
-
node.callback.apply(node.context || this, args);
|
171
|
-
}
|
172
|
-
}
|
173
|
-
}
|
174
|
-
|
175
|
-
return this;
|
176
|
-
}
|
177
|
-
|
178
|
-
};
|
179
|
-
|
180
|
-
// Aliases for backwards compatibility.
|
181
|
-
Events.bind = Events.on;
|
182
|
-
Events.unbind = Events.off;
|
183
|
-
|
184
|
-
// Backbone.Model
|
185
|
-
// --------------
|
186
|
-
|
187
|
-
// Create a new model, with defined attributes. A client id (`cid`)
|
188
|
-
// is automatically generated and assigned for you.
|
189
|
-
var Model = Backbone.Model = function(attributes, options) {
|
190
|
-
var defaults;
|
191
|
-
attributes || (attributes = {});
|
192
|
-
if (options && options.parse) attributes = this.parse(attributes);
|
193
|
-
if (defaults = getValue(this, 'defaults')) {
|
194
|
-
attributes = _.extend({}, defaults, attributes);
|
195
|
-
}
|
196
|
-
if (options && options.collection) this.collection = options.collection;
|
197
|
-
this.attributes = {};
|
198
|
-
this._escapedAttributes = {};
|
199
|
-
this.cid = _.uniqueId('c');
|
200
|
-
this.changed = {};
|
201
|
-
this._silent = {};
|
202
|
-
this._pending = {};
|
203
|
-
this.set(attributes, {silent: true});
|
204
|
-
// Reset change tracking.
|
205
|
-
this.changed = {};
|
206
|
-
this._silent = {};
|
207
|
-
this._pending = {};
|
208
|
-
this._previousAttributes = _.clone(this.attributes);
|
209
|
-
this.initialize.apply(this, arguments);
|
210
|
-
};
|
211
|
-
|
212
|
-
// Attach all inheritable methods to the Model prototype.
|
213
|
-
_.extend(Model.prototype, Events, {
|
214
|
-
|
215
|
-
// A hash of attributes whose current and previous value differ.
|
216
|
-
changed: null,
|
217
|
-
|
218
|
-
// A hash of attributes that have silently changed since the last time
|
219
|
-
// `change` was called. Will become pending attributes on the next call.
|
220
|
-
_silent: null,
|
221
|
-
|
222
|
-
// A hash of attributes that have changed since the last `'change'` event
|
223
|
-
// began.
|
224
|
-
_pending: null,
|
225
|
-
|
226
|
-
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
227
|
-
// CouchDB users may want to set this to `"_id"`.
|
228
|
-
idAttribute: 'id',
|
229
|
-
|
230
|
-
// Initialize is an empty function by default. Override it with your own
|
231
|
-
// initialization logic.
|
232
|
-
initialize: function(){},
|
233
|
-
|
234
|
-
// Return a copy of the model's `attributes` object.
|
235
|
-
toJSON: function(options) {
|
236
|
-
return _.clone(this.attributes);
|
237
|
-
},
|
238
|
-
|
239
|
-
// Get the value of an attribute.
|
240
|
-
get: function(attr) {
|
241
|
-
return this.attributes[attr];
|
242
|
-
},
|
243
|
-
|
244
|
-
// Get the HTML-escaped value of an attribute.
|
245
|
-
escape: function(attr) {
|
246
|
-
var html;
|
247
|
-
if (html = this._escapedAttributes[attr]) return html;
|
248
|
-
var val = this.get(attr);
|
249
|
-
return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val);
|
250
|
-
},
|
251
|
-
|
252
|
-
// Returns `true` if the attribute contains a value that is not null
|
253
|
-
// or undefined.
|
254
|
-
has: function(attr) {
|
255
|
-
return this.get(attr) != null;
|
256
|
-
},
|
257
|
-
|
258
|
-
// Set a hash of model attributes on the object, firing `"change"` unless
|
259
|
-
// you choose to silence it.
|
260
|
-
set: function(key, value, options) {
|
261
|
-
var attrs, attr, val;
|
262
|
-
|
263
|
-
// Handle both `"key", value` and `{key: value}` -style arguments.
|
264
|
-
if (_.isObject(key) || key == null) {
|
265
|
-
attrs = key;
|
266
|
-
options = value;
|
267
|
-
} else {
|
268
|
-
attrs = {};
|
269
|
-
attrs[key] = value;
|
270
|
-
}
|
271
|
-
|
272
|
-
// Extract attributes and options.
|
273
|
-
options || (options = {});
|
274
|
-
if (!attrs) return this;
|
275
|
-
if (attrs instanceof Model) attrs = attrs.attributes;
|
276
|
-
if (options.unset) for (attr in attrs) attrs[attr] = void 0;
|
277
|
-
|
278
|
-
// Run validation.
|
279
|
-
if (!this._validate(attrs, options)) return false;
|
280
|
-
|
281
|
-
// Check for changes of `id`.
|
282
|
-
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
283
|
-
|
284
|
-
var changes = options.changes = {};
|
285
|
-
var now = this.attributes;
|
286
|
-
var escaped = this._escapedAttributes;
|
287
|
-
var prev = this._previousAttributes || {};
|
288
|
-
|
289
|
-
// For each `set` attribute...
|
290
|
-
for (attr in attrs) {
|
291
|
-
val = attrs[attr];
|
292
|
-
|
293
|
-
// If the new and current value differ, record the change.
|
294
|
-
if (!_.isEqual(now[attr], val) || (options.unset && _.has(now, attr))) {
|
295
|
-
delete escaped[attr];
|
296
|
-
(options.silent ? this._silent : changes)[attr] = true;
|
297
|
-
}
|
298
|
-
|
299
|
-
// Update or delete the current value.
|
300
|
-
options.unset ? delete now[attr] : now[attr] = val;
|
301
|
-
|
302
|
-
// If the new and previous value differ, record the change. If not,
|
303
|
-
// then remove changes for this attribute.
|
304
|
-
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) {
|
305
|
-
this.changed[attr] = val;
|
306
|
-
if (!options.silent) this._pending[attr] = true;
|
307
|
-
} else {
|
308
|
-
delete this.changed[attr];
|
309
|
-
delete this._pending[attr];
|
310
|
-
}
|
311
|
-
}
|
312
|
-
|
313
|
-
// Fire the `"change"` events.
|
314
|
-
if (!options.silent) this.change(options);
|
315
|
-
return this;
|
316
|
-
},
|
317
|
-
|
318
|
-
// Remove an attribute from the model, firing `"change"` unless you choose
|
319
|
-
// to silence it. `unset` is a noop if the attribute doesn't exist.
|
320
|
-
unset: function(attr, options) {
|
321
|
-
(options || (options = {})).unset = true;
|
322
|
-
return this.set(attr, null, options);
|
323
|
-
},
|
324
|
-
|
325
|
-
// Clear all attributes on the model, firing `"change"` unless you choose
|
326
|
-
// to silence it.
|
327
|
-
clear: function(options) {
|
328
|
-
(options || (options = {})).unset = true;
|
329
|
-
return this.set(_.clone(this.attributes), options);
|
330
|
-
},
|
331
|
-
|
332
|
-
// Fetch the model from the server. If the server's representation of the
|
333
|
-
// model differs from its current attributes, they will be overriden,
|
334
|
-
// triggering a `"change"` event.
|
335
|
-
fetch: function(options) {
|
336
|
-
options = options ? _.clone(options) : {};
|
337
|
-
var model = this;
|
338
|
-
var success = options.success;
|
339
|
-
options.success = function(resp, status, xhr) {
|
340
|
-
if (!model.set(model.parse(resp, xhr), options)) return false;
|
341
|
-
if (success) success(model, resp);
|
342
|
-
};
|
343
|
-
options.error = Backbone.wrapError(options.error, model, options);
|
344
|
-
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
345
|
-
},
|
346
|
-
|
347
|
-
// Set a hash of model attributes, and sync the model to the server.
|
348
|
-
// If the server returns an attributes hash that differs, the model's
|
349
|
-
// state will be `set` again.
|
350
|
-
save: function(key, value, options) {
|
351
|
-
var attrs, current;
|
352
|
-
|
353
|
-
// Handle both `("key", value)` and `({key: value})` -style calls.
|
354
|
-
if (_.isObject(key) || key == null) {
|
355
|
-
attrs = key;
|
356
|
-
options = value;
|
357
|
-
} else {
|
358
|
-
attrs = {};
|
359
|
-
attrs[key] = value;
|
360
|
-
}
|
361
|
-
options = options ? _.clone(options) : {};
|
362
|
-
|
363
|
-
// If we're "wait"-ing to set changed attributes, validate early.
|
364
|
-
if (options.wait) {
|
365
|
-
if (!this._validate(attrs, options)) return false;
|
366
|
-
current = _.clone(this.attributes);
|
367
|
-
}
|
368
|
-
|
369
|
-
// Regular saves `set` attributes before persisting to the server.
|
370
|
-
var silentOptions = _.extend({}, options, {silent: true});
|
371
|
-
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
|
372
|
-
return false;
|
373
|
-
}
|
374
|
-
|
375
|
-
// After a successful server-side save, the client is (optionally)
|
376
|
-
// updated with the server-side state.
|
377
|
-
var model = this;
|
378
|
-
var success = options.success;
|
379
|
-
options.success = function(resp, status, xhr) {
|
380
|
-
var serverAttrs = model.parse(resp, xhr);
|
381
|
-
if (options.wait) {
|
382
|
-
delete options.wait;
|
383
|
-
serverAttrs = _.extend(attrs || {}, serverAttrs);
|
384
|
-
}
|
385
|
-
if (!model.set(serverAttrs, options)) return false;
|
386
|
-
if (success) {
|
387
|
-
success(model, resp);
|
388
|
-
} else {
|
389
|
-
model.trigger('sync', model, resp, options);
|
390
|
-
}
|
391
|
-
};
|
392
|
-
|
393
|
-
// Finish configuring and sending the Ajax request.
|
394
|
-
options.error = Backbone.wrapError(options.error, model, options);
|
395
|
-
var method = this.isNew() ? 'create' : 'update';
|
396
|
-
var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
|
397
|
-
if (options.wait) this.set(current, silentOptions);
|
398
|
-
return xhr;
|
399
|
-
},
|
400
|
-
|
401
|
-
// Destroy this model on the server if it was already persisted.
|
402
|
-
// Optimistically removes the model from its collection, if it has one.
|
403
|
-
// If `wait: true` is passed, waits for the server to respond before removal.
|
404
|
-
destroy: function(options) {
|
405
|
-
options = options ? _.clone(options) : {};
|
406
|
-
var model = this;
|
407
|
-
var success = options.success;
|
408
|
-
|
409
|
-
var triggerDestroy = function() {
|
410
|
-
model.trigger('destroy', model, model.collection, options);
|
411
|
-
};
|
412
|
-
|
413
|
-
if (this.isNew()) {
|
414
|
-
triggerDestroy();
|
415
|
-
return false;
|
416
|
-
}
|
417
|
-
|
418
|
-
options.success = function(resp) {
|
419
|
-
if (options.wait) triggerDestroy();
|
420
|
-
if (success) {
|
421
|
-
success(model, resp);
|
422
|
-
} else {
|
423
|
-
model.trigger('sync', model, resp, options);
|
424
|
-
}
|
425
|
-
};
|
426
|
-
|
427
|
-
options.error = Backbone.wrapError(options.error, model, options);
|
428
|
-
var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options);
|
429
|
-
if (!options.wait) triggerDestroy();
|
430
|
-
return xhr;
|
431
|
-
},
|
432
|
-
|
433
|
-
// Default URL for the model's representation on the server -- if you're
|
434
|
-
// using Backbone's restful methods, override this to change the endpoint
|
435
|
-
// that will be called.
|
436
|
-
url: function() {
|
437
|
-
var base = getValue(this, 'urlRoot') || getValue(this.collection, 'url') || urlError();
|
438
|
-
if (this.isNew()) return base;
|
439
|
-
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
440
|
-
},
|
441
|
-
|
442
|
-
// **parse** converts a response into the hash of attributes to be `set` on
|
443
|
-
// the model. The default implementation is just to pass the response along.
|
444
|
-
parse: function(resp, xhr) {
|
445
|
-
return resp;
|
446
|
-
},
|
447
|
-
|
448
|
-
// Create a new model with identical attributes to this one.
|
449
|
-
clone: function() {
|
450
|
-
return new this.constructor(this.attributes);
|
451
|
-
},
|
452
|
-
|
453
|
-
// A model is new if it has never been saved to the server, and lacks an id.
|
454
|
-
isNew: function() {
|
455
|
-
return this.id == null;
|
456
|
-
},
|
457
|
-
|
458
|
-
// Call this method to manually fire a `"change"` event for this model and
|
459
|
-
// a `"change:attribute"` event for each changed attribute.
|
460
|
-
// Calling this will cause all objects observing the model to update.
|
461
|
-
change: function(options) {
|
462
|
-
options || (options = {});
|
463
|
-
var changing = this._changing;
|
464
|
-
this._changing = true;
|
465
|
-
|
466
|
-
// Silent changes become pending changes.
|
467
|
-
for (var attr in this._silent) this._pending[attr] = true;
|
468
|
-
|
469
|
-
// Silent changes are triggered.
|
470
|
-
var changes = _.extend({}, options.changes, this._silent);
|
471
|
-
this._silent = {};
|
472
|
-
for (var attr in changes) {
|
473
|
-
this.trigger('change:' + attr, this, this.get(attr), options);
|
474
|
-
}
|
475
|
-
if (changing) return this;
|
476
|
-
|
477
|
-
// Continue firing `"change"` events while there are pending changes.
|
478
|
-
while (!_.isEmpty(this._pending)) {
|
479
|
-
this._pending = {};
|
480
|
-
this.trigger('change', this, options);
|
481
|
-
// Pending and silent changes still remain.
|
482
|
-
for (var attr in this.changed) {
|
483
|
-
if (this._pending[attr] || this._silent[attr]) continue;
|
484
|
-
delete this.changed[attr];
|
485
|
-
}
|
486
|
-
this._previousAttributes = _.clone(this.attributes);
|
487
|
-
}
|
488
|
-
|
489
|
-
this._changing = false;
|
490
|
-
return this;
|
491
|
-
},
|
492
|
-
|
493
|
-
// Determine if the model has changed since the last `"change"` event.
|
494
|
-
// If you specify an attribute name, determine if that attribute has changed.
|
495
|
-
hasChanged: function(attr) {
|
496
|
-
if (!arguments.length) return !_.isEmpty(this.changed);
|
497
|
-
return _.has(this.changed, attr);
|
498
|
-
},
|
499
|
-
|
500
|
-
// Return an object containing all the attributes that have changed, or
|
501
|
-
// false if there are no changed attributes. Useful for determining what
|
502
|
-
// parts of a view need to be updated and/or what attributes need to be
|
503
|
-
// persisted to the server. Unset attributes will be set to undefined.
|
504
|
-
// You can also pass an attributes object to diff against the model,
|
505
|
-
// determining if there *would be* a change.
|
506
|
-
changedAttributes: function(diff) {
|
507
|
-
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
|
508
|
-
var val, changed = false, old = this._previousAttributes;
|
509
|
-
for (var attr in diff) {
|
510
|
-
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
511
|
-
(changed || (changed = {}))[attr] = val;
|
512
|
-
}
|
513
|
-
return changed;
|
514
|
-
},
|
515
|
-
|
516
|
-
// Get the previous value of an attribute, recorded at the time the last
|
517
|
-
// `"change"` event was fired.
|
518
|
-
previous: function(attr) {
|
519
|
-
if (!arguments.length || !this._previousAttributes) return null;
|
520
|
-
return this._previousAttributes[attr];
|
521
|
-
},
|
522
|
-
|
523
|
-
// Get all of the attributes of the model at the time of the previous
|
524
|
-
// `"change"` event.
|
525
|
-
previousAttributes: function() {
|
526
|
-
return _.clone(this._previousAttributes);
|
527
|
-
},
|
528
|
-
|
529
|
-
// Check if the model is currently in a valid state. It's only possible to
|
530
|
-
// get into an *invalid* state if you're using silent changes.
|
531
|
-
isValid: function() {
|
532
|
-
return !this.validate(this.attributes);
|
533
|
-
},
|
534
|
-
|
535
|
-
// Run validation against the next complete set of model attributes,
|
536
|
-
// returning `true` if all is well. If a specific `error` callback has
|
537
|
-
// been passed, call that instead of firing the general `"error"` event.
|
538
|
-
_validate: function(attrs, options) {
|
539
|
-
if (options.silent || !this.validate) return true;
|
540
|
-
attrs = _.extend({}, this.attributes, attrs);
|
541
|
-
var error = this.validate(attrs, options);
|
542
|
-
if (!error) return true;
|
543
|
-
if (options && options.error) {
|
544
|
-
options.error(this, error, options);
|
545
|
-
} else {
|
546
|
-
this.trigger('error', this, error, options);
|
547
|
-
}
|
548
|
-
return false;
|
549
|
-
}
|
550
|
-
|
551
|
-
});
|
552
|
-
|
553
|
-
// Backbone.Collection
|
554
|
-
// -------------------
|
555
|
-
|
556
|
-
// Provides a standard collection class for our sets of models, ordered
|
557
|
-
// or unordered. If a `comparator` is specified, the Collection will maintain
|
558
|
-
// its models in sort order, as they're added and removed.
|
559
|
-
var Collection = Backbone.Collection = function(models, options) {
|
560
|
-
options || (options = {});
|
561
|
-
if (options.model) this.model = options.model;
|
562
|
-
if (options.comparator) this.comparator = options.comparator;
|
563
|
-
this._reset();
|
564
|
-
this.initialize.apply(this, arguments);
|
565
|
-
if (models) this.reset(models, {silent: true, parse: options.parse});
|
566
|
-
};
|
567
|
-
|
568
|
-
// Define the Collection's inheritable methods.
|
569
|
-
_.extend(Collection.prototype, Events, {
|
570
|
-
|
571
|
-
// The default model for a collection is just a **Backbone.Model**.
|
572
|
-
// This should be overridden in most cases.
|
573
|
-
model: Model,
|
574
|
-
|
575
|
-
// Initialize is an empty function by default. Override it with your own
|
576
|
-
// initialization logic.
|
577
|
-
initialize: function(){},
|
578
|
-
|
579
|
-
// The JSON representation of a Collection is an array of the
|
580
|
-
// models' attributes.
|
581
|
-
toJSON: function(options) {
|
582
|
-
return this.map(function(model){ return model.toJSON(options); });
|
583
|
-
},
|
584
|
-
|
585
|
-
// Add a model, or list of models to the set. Pass **silent** to avoid
|
586
|
-
// firing the `add` event for every new model.
|
587
|
-
add: function(models, options) {
|
588
|
-
var i, index, length, model, cid, id, cids = {}, ids = {}, dups = [];
|
589
|
-
options || (options = {});
|
590
|
-
models = _.isArray(models) ? models.slice() : [models];
|
591
|
-
|
592
|
-
// Begin by turning bare objects into model references, and preventing
|
593
|
-
// invalid models or duplicate models from being added.
|
594
|
-
for (i = 0, length = models.length; i < length; i++) {
|
595
|
-
if (!(model = models[i] = this._prepareModel(models[i], options))) {
|
596
|
-
throw new Error("Can't add an invalid model to a collection");
|
597
|
-
}
|
598
|
-
cid = model.cid;
|
599
|
-
id = model.id;
|
600
|
-
if (cids[cid] || this._byCid[cid] || ((id != null) && (ids[id] || this._byId[id]))) {
|
601
|
-
dups.push(i);
|
602
|
-
continue;
|
603
|
-
}
|
604
|
-
cids[cid] = ids[id] = model;
|
605
|
-
}
|
606
|
-
|
607
|
-
// Remove duplicates.
|
608
|
-
i = dups.length;
|
609
|
-
while (i--) {
|
610
|
-
models.splice(dups[i], 1);
|
611
|
-
}
|
612
|
-
|
613
|
-
// Listen to added models' events, and index models for lookup by
|
614
|
-
// `id` and by `cid`.
|
615
|
-
for (i = 0, length = models.length; i < length; i++) {
|
616
|
-
(model = models[i]).on('all', this._onModelEvent, this);
|
617
|
-
this._byCid[model.cid] = model;
|
618
|
-
if (model.id != null) this._byId[model.id] = model;
|
619
|
-
}
|
620
|
-
|
621
|
-
// Insert models into the collection, re-sorting if needed, and triggering
|
622
|
-
// `add` events unless silenced.
|
623
|
-
this.length += length;
|
624
|
-
index = options.at != null ? options.at : this.models.length;
|
625
|
-
splice.apply(this.models, [index, 0].concat(models));
|
626
|
-
if (this.comparator) this.sort({silent: true});
|
627
|
-
if (options.silent) return this;
|
628
|
-
for (i = 0, length = this.models.length; i < length; i++) {
|
629
|
-
if (!cids[(model = this.models[i]).cid]) continue;
|
630
|
-
options.index = i;
|
631
|
-
model.trigger('add', model, this, options);
|
632
|
-
}
|
633
|
-
return this;
|
634
|
-
},
|
635
|
-
|
636
|
-
// Remove a model, or a list of models from the set. Pass silent to avoid
|
637
|
-
// firing the `remove` event for every model removed.
|
638
|
-
remove: function(models, options) {
|
639
|
-
var i, l, index, model;
|
640
|
-
options || (options = {});
|
641
|
-
models = _.isArray(models) ? models.slice() : [models];
|
642
|
-
for (i = 0, l = models.length; i < l; i++) {
|
643
|
-
model = this.getByCid(models[i]) || this.get(models[i]);
|
644
|
-
if (!model) continue;
|
645
|
-
delete this._byId[model.id];
|
646
|
-
delete this._byCid[model.cid];
|
647
|
-
index = this.indexOf(model);
|
648
|
-
this.models.splice(index, 1);
|
649
|
-
this.length--;
|
650
|
-
if (!options.silent) {
|
651
|
-
options.index = index;
|
652
|
-
model.trigger('remove', model, this, options);
|
653
|
-
}
|
654
|
-
this._removeReference(model);
|
655
|
-
}
|
656
|
-
return this;
|
657
|
-
},
|
658
|
-
|
659
|
-
// Add a model to the end of the collection.
|
660
|
-
push: function(model, options) {
|
661
|
-
model = this._prepareModel(model, options);
|
662
|
-
this.add(model, options);
|
663
|
-
return model;
|
664
|
-
},
|
665
|
-
|
666
|
-
// Remove a model from the end of the collection.
|
667
|
-
pop: function(options) {
|
668
|
-
var model = this.at(this.length - 1);
|
669
|
-
this.remove(model, options);
|
670
|
-
return model;
|
671
|
-
},
|
672
|
-
|
673
|
-
// Add a model to the beginning of the collection.
|
674
|
-
unshift: function(model, options) {
|
675
|
-
model = this._prepareModel(model, options);
|
676
|
-
this.add(model, _.extend({at: 0}, options));
|
677
|
-
return model;
|
678
|
-
},
|
679
|
-
|
680
|
-
// Remove a model from the beginning of the collection.
|
681
|
-
shift: function(options) {
|
682
|
-
var model = this.at(0);
|
683
|
-
this.remove(model, options);
|
684
|
-
return model;
|
685
|
-
},
|
686
|
-
|
687
|
-
// Get a model from the set by id.
|
688
|
-
get: function(id) {
|
689
|
-
if (id == null) return void 0;
|
690
|
-
return this._byId[id.id != null ? id.id : id];
|
691
|
-
},
|
692
|
-
|
693
|
-
// Get a model from the set by client id.
|
694
|
-
getByCid: function(cid) {
|
695
|
-
return cid && this._byCid[cid.cid || cid];
|
696
|
-
},
|
697
|
-
|
698
|
-
// Get the model at the given index.
|
699
|
-
at: function(index) {
|
700
|
-
return this.models[index];
|
701
|
-
},
|
702
|
-
|
703
|
-
// Return models with matching attributes. Useful for simple cases of `filter`.
|
704
|
-
where: function(attrs) {
|
705
|
-
if (_.isEmpty(attrs)) return [];
|
706
|
-
return this.filter(function(model) {
|
707
|
-
for (var key in attrs) {
|
708
|
-
if (attrs[key] !== model.get(key)) return false;
|
709
|
-
}
|
710
|
-
return true;
|
711
|
-
});
|
712
|
-
},
|
713
|
-
|
714
|
-
// Force the collection to re-sort itself. You don't need to call this under
|
715
|
-
// normal circumstances, as the set will maintain sort order as each item
|
716
|
-
// is added.
|
717
|
-
sort: function(options) {
|
718
|
-
options || (options = {});
|
719
|
-
if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
|
720
|
-
var boundComparator = _.bind(this.comparator, this);
|
721
|
-
if (this.comparator.length == 1) {
|
722
|
-
this.models = this.sortBy(boundComparator);
|
723
|
-
} else {
|
724
|
-
this.models.sort(boundComparator);
|
725
|
-
}
|
726
|
-
if (!options.silent) this.trigger('reset', this, options);
|
727
|
-
return this;
|
728
|
-
},
|
729
|
-
|
730
|
-
// Pluck an attribute from each model in the collection.
|
731
|
-
pluck: function(attr) {
|
732
|
-
return _.map(this.models, function(model){ return model.get(attr); });
|
733
|
-
},
|
734
|
-
|
735
|
-
// When you have more items than you want to add or remove individually,
|
736
|
-
// you can reset the entire set with a new list of models, without firing
|
737
|
-
// any `add` or `remove` events. Fires `reset` when finished.
|
738
|
-
reset: function(models, options) {
|
739
|
-
models || (models = []);
|
740
|
-
options || (options = {});
|
741
|
-
for (var i = 0, l = this.models.length; i < l; i++) {
|
742
|
-
this._removeReference(this.models[i]);
|
743
|
-
}
|
744
|
-
this._reset();
|
745
|
-
this.add(models, _.extend({silent: true}, options));
|
746
|
-
if (!options.silent) this.trigger('reset', this, options);
|
747
|
-
return this;
|
748
|
-
},
|
749
|
-
|
750
|
-
// Fetch the default set of models for this collection, resetting the
|
751
|
-
// collection when they arrive. If `add: true` is passed, appends the
|
752
|
-
// models to the collection instead of resetting.
|
753
|
-
fetch: function(options) {
|
754
|
-
options = options ? _.clone(options) : {};
|
755
|
-
if (options.parse === undefined) options.parse = true;
|
756
|
-
var collection = this;
|
757
|
-
var success = options.success;
|
758
|
-
options.success = function(resp, status, xhr) {
|
759
|
-
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
|
760
|
-
if (success) success(collection, resp);
|
761
|
-
};
|
762
|
-
options.error = Backbone.wrapError(options.error, collection, options);
|
763
|
-
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
764
|
-
},
|
765
|
-
|
766
|
-
// Create a new instance of a model in this collection. Add the model to the
|
767
|
-
// collection immediately, unless `wait: true` is passed, in which case we
|
768
|
-
// wait for the server to agree.
|
769
|
-
create: function(model, options) {
|
770
|
-
var coll = this;
|
771
|
-
options = options ? _.clone(options) : {};
|
772
|
-
model = this._prepareModel(model, options);
|
773
|
-
if (!model) return false;
|
774
|
-
if (!options.wait) coll.add(model, options);
|
775
|
-
var success = options.success;
|
776
|
-
options.success = function(nextModel, resp, xhr) {
|
777
|
-
if (options.wait) coll.add(nextModel, options);
|
778
|
-
if (success) {
|
779
|
-
success(nextModel, resp);
|
780
|
-
} else {
|
781
|
-
nextModel.trigger('sync', model, resp, options);
|
782
|
-
}
|
783
|
-
};
|
784
|
-
model.save(null, options);
|
785
|
-
return model;
|
786
|
-
},
|
787
|
-
|
788
|
-
// **parse** converts a response into a list of models to be added to the
|
789
|
-
// collection. The default implementation is just to pass it through.
|
790
|
-
parse: function(resp, xhr) {
|
791
|
-
return resp;
|
792
|
-
},
|
793
|
-
|
794
|
-
// Proxy to _'s chain. Can't be proxied the same way the rest of the
|
795
|
-
// underscore methods are proxied because it relies on the underscore
|
796
|
-
// constructor.
|
797
|
-
chain: function () {
|
798
|
-
return _(this.models).chain();
|
799
|
-
},
|
800
|
-
|
801
|
-
// Reset all internal state. Called when the collection is reset.
|
802
|
-
_reset: function(options) {
|
803
|
-
this.length = 0;
|
804
|
-
this.models = [];
|
805
|
-
this._byId = {};
|
806
|
-
this._byCid = {};
|
807
|
-
},
|
808
|
-
|
809
|
-
// Prepare a model or hash of attributes to be added to this collection.
|
810
|
-
_prepareModel: function(model, options) {
|
811
|
-
options || (options = {});
|
812
|
-
if (!(model instanceof Model)) {
|
813
|
-
var attrs = model;
|
814
|
-
options.collection = this;
|
815
|
-
model = new this.model(attrs, options);
|
816
|
-
if (!model._validate(model.attributes, options)) model = false;
|
817
|
-
} else if (!model.collection) {
|
818
|
-
model.collection = this;
|
819
|
-
}
|
820
|
-
return model;
|
821
|
-
},
|
822
|
-
|
823
|
-
// Internal method to remove a model's ties to a collection.
|
824
|
-
_removeReference: function(model) {
|
825
|
-
if (this == model.collection) {
|
826
|
-
delete model.collection;
|
827
|
-
}
|
828
|
-
model.off('all', this._onModelEvent, this);
|
829
|
-
},
|
830
|
-
|
831
|
-
// Internal method called every time a model in the set fires an event.
|
832
|
-
// Sets need to update their indexes when models change ids. All other
|
833
|
-
// events simply proxy through. "add" and "remove" events that originate
|
834
|
-
// in other collections are ignored.
|
835
|
-
_onModelEvent: function(event, model, collection, options) {
|
836
|
-
if ((event == 'add' || event == 'remove') && collection != this) return;
|
837
|
-
if (event == 'destroy') {
|
838
|
-
this.remove(model, options);
|
839
|
-
}
|
840
|
-
if (model && event === 'change:' + model.idAttribute) {
|
841
|
-
delete this._byId[model.previous(model.idAttribute)];
|
842
|
-
this._byId[model.id] = model;
|
843
|
-
}
|
844
|
-
this.trigger.apply(this, arguments);
|
845
|
-
}
|
846
|
-
|
847
|
-
});
|
848
|
-
|
849
|
-
// Underscore methods that we want to implement on the Collection.
|
850
|
-
var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
|
851
|
-
'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
|
852
|
-
'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
|
853
|
-
'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
|
854
|
-
'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy'];
|
855
|
-
|
856
|
-
// Mix in each Underscore method as a proxy to `Collection#models`.
|
857
|
-
_.each(methods, function(method) {
|
858
|
-
Collection.prototype[method] = function() {
|
859
|
-
return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
|
860
|
-
};
|
861
|
-
});
|
862
|
-
|
863
|
-
// Backbone.Router
|
864
|
-
// -------------------
|
865
|
-
|
866
|
-
// Routers map faux-URLs to actions, and fire events when routes are
|
867
|
-
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
868
|
-
var Router = Backbone.Router = function(options) {
|
869
|
-
options || (options = {});
|
870
|
-
if (options.routes) this.routes = options.routes;
|
871
|
-
this._bindRoutes();
|
872
|
-
this.initialize.apply(this, arguments);
|
873
|
-
};
|
874
|
-
|
875
|
-
// Cached regular expressions for matching named param parts and splatted
|
876
|
-
// parts of route strings.
|
877
|
-
var namedParam = /:\w+/g;
|
878
|
-
var splatParam = /\*\w+/g;
|
879
|
-
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
|
880
|
-
|
881
|
-
// Set up all inheritable **Backbone.Router** properties and methods.
|
882
|
-
_.extend(Router.prototype, Events, {
|
883
|
-
|
884
|
-
// Initialize is an empty function by default. Override it with your own
|
885
|
-
// initialization logic.
|
886
|
-
initialize: function(){},
|
887
|
-
|
888
|
-
// Manually bind a single named route to a callback. For example:
|
889
|
-
//
|
890
|
-
// this.route('search/:query/p:num', 'search', function(query, num) {
|
891
|
-
// ...
|
892
|
-
// });
|
893
|
-
//
|
894
|
-
route: function(route, name, callback) {
|
895
|
-
Backbone.history || (Backbone.history = new History);
|
896
|
-
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
|
897
|
-
if (!callback) callback = this[name];
|
898
|
-
Backbone.history.route(route, _.bind(function(fragment) {
|
899
|
-
var args = this._extractParameters(route, fragment);
|
900
|
-
callback && callback.apply(this, args);
|
901
|
-
this.trigger.apply(this, ['route:' + name].concat(args));
|
902
|
-
Backbone.history.trigger('route', this, name, args);
|
903
|
-
}, this));
|
904
|
-
return this;
|
905
|
-
},
|
906
|
-
|
907
|
-
// Simple proxy to `Backbone.history` to save a fragment into the history.
|
908
|
-
navigate: function(fragment, options) {
|
909
|
-
Backbone.history.navigate(fragment, options);
|
910
|
-
},
|
911
|
-
|
912
|
-
// Bind all defined routes to `Backbone.history`. We have to reverse the
|
913
|
-
// order of the routes here to support behavior where the most general
|
914
|
-
// routes can be defined at the bottom of the route map.
|
915
|
-
_bindRoutes: function() {
|
916
|
-
if (!this.routes) return;
|
917
|
-
var routes = [];
|
918
|
-
for (var route in this.routes) {
|
919
|
-
routes.unshift([route, this.routes[route]]);
|
920
|
-
}
|
921
|
-
for (var i = 0, l = routes.length; i < l; i++) {
|
922
|
-
this.route(routes[i][0], routes[i][1], this[routes[i][1]]);
|
923
|
-
}
|
924
|
-
},
|
925
|
-
|
926
|
-
// Convert a route string into a regular expression, suitable for matching
|
927
|
-
// against the current location hash.
|
928
|
-
_routeToRegExp: function(route) {
|
929
|
-
route = route.replace(escapeRegExp, '\\$&')
|
930
|
-
.replace(namedParam, '([^\/]+)')
|
931
|
-
.replace(splatParam, '(.*?)');
|
932
|
-
return new RegExp('^' + route + '$');
|
933
|
-
},
|
934
|
-
|
935
|
-
// Given a route, and a URL fragment that it matches, return the array of
|
936
|
-
// extracted parameters.
|
937
|
-
_extractParameters: function(route, fragment) {
|
938
|
-
return route.exec(fragment).slice(1);
|
939
|
-
}
|
940
|
-
|
941
|
-
});
|
942
|
-
|
943
|
-
// Backbone.History
|
944
|
-
// ----------------
|
945
|
-
|
946
|
-
// Handles cross-browser history management, based on URL fragments. If the
|
947
|
-
// browser does not support `onhashchange`, falls back to polling.
|
948
|
-
var History = Backbone.History = function() {
|
949
|
-
this.handlers = [];
|
950
|
-
_.bindAll(this, 'checkUrl');
|
951
|
-
};
|
952
|
-
|
953
|
-
// Cached regex for cleaning leading hashes and slashes .
|
954
|
-
var routeStripper = /^[#\/]/;
|
955
|
-
|
956
|
-
// Cached regex for detecting MSIE.
|
957
|
-
var isExplorer = /msie [\w.]+/;
|
958
|
-
|
959
|
-
// Has the history handling already been started?
|
960
|
-
History.started = false;
|
961
|
-
|
962
|
-
// Set up all inheritable **Backbone.History** properties and methods.
|
963
|
-
_.extend(History.prototype, Events, {
|
964
|
-
|
965
|
-
// The default interval to poll for hash changes, if necessary, is
|
966
|
-
// twenty times a second.
|
967
|
-
interval: 50,
|
968
|
-
|
969
|
-
// Gets the true hash value. Cannot use location.hash directly due to bug
|
970
|
-
// in Firefox where location.hash will always be decoded.
|
971
|
-
getHash: function(windowOverride) {
|
972
|
-
var loc = windowOverride ? windowOverride.location : window.location;
|
973
|
-
var match = loc.href.match(/#(.*)$/);
|
974
|
-
return match ? match[1] : '';
|
975
|
-
},
|
976
|
-
|
977
|
-
// Get the cross-browser normalized URL fragment, either from the URL,
|
978
|
-
// the hash, or the override.
|
979
|
-
getFragment: function(fragment, forcePushState) {
|
980
|
-
if (fragment == null) {
|
981
|
-
if (this._hasPushState || forcePushState) {
|
982
|
-
fragment = window.location.pathname;
|
983
|
-
var search = window.location.search;
|
984
|
-
if (search) fragment += search;
|
985
|
-
} else {
|
986
|
-
fragment = this.getHash();
|
987
|
-
}
|
988
|
-
}
|
989
|
-
if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
|
990
|
-
return fragment.replace(routeStripper, '');
|
991
|
-
},
|
992
|
-
|
993
|
-
// Start the hash change handling, returning `true` if the current URL matches
|
994
|
-
// an existing route, and `false` otherwise.
|
995
|
-
start: function(options) {
|
996
|
-
if (History.started) throw new Error("Backbone.history has already been started");
|
997
|
-
History.started = true;
|
998
|
-
|
999
|
-
// Figure out the initial configuration. Do we need an iframe?
|
1000
|
-
// Is pushState desired ... is it available?
|
1001
|
-
this.options = _.extend({}, {root: '/'}, this.options, options);
|
1002
|
-
this._wantsHashChange = this.options.hashChange !== false;
|
1003
|
-
this._wantsPushState = !!this.options.pushState;
|
1004
|
-
this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
|
1005
|
-
var fragment = this.getFragment();
|
1006
|
-
var docMode = document.documentMode;
|
1007
|
-
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
|
1008
|
-
|
1009
|
-
if (oldIE) {
|
1010
|
-
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
1011
|
-
this.navigate(fragment);
|
1012
|
-
}
|
1013
|
-
|
1014
|
-
// Depending on whether we're using pushState or hashes, and whether
|
1015
|
-
// 'onhashchange' is supported, determine how we check the URL state.
|
1016
|
-
if (this._hasPushState) {
|
1017
|
-
$(window).bind('popstate', this.checkUrl);
|
1018
|
-
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
|
1019
|
-
$(window).bind('hashchange', this.checkUrl);
|
1020
|
-
} else if (this._wantsHashChange) {
|
1021
|
-
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
|
1022
|
-
}
|
1023
|
-
|
1024
|
-
// Determine if we need to change the base url, for a pushState link
|
1025
|
-
// opened by a non-pushState browser.
|
1026
|
-
this.fragment = fragment;
|
1027
|
-
var loc = window.location;
|
1028
|
-
var atRoot = loc.pathname == this.options.root;
|
1029
|
-
|
1030
|
-
// If we've started off with a route from a `pushState`-enabled browser,
|
1031
|
-
// but we're currently in a browser that doesn't support it...
|
1032
|
-
if (this._wantsHashChange && this._wantsPushState && !this._hasPushState && !atRoot) {
|
1033
|
-
this.fragment = this.getFragment(null, true);
|
1034
|
-
window.location.replace(this.options.root + '#' + this.fragment);
|
1035
|
-
// Return immediately as browser will do redirect to new url
|
1036
|
-
return true;
|
1037
|
-
|
1038
|
-
// Or if we've started out with a hash-based route, but we're currently
|
1039
|
-
// in a browser where it could be `pushState`-based instead...
|
1040
|
-
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
|
1041
|
-
this.fragment = this.getHash().replace(routeStripper, '');
|
1042
|
-
window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
|
1043
|
-
}
|
1044
|
-
|
1045
|
-
if (!this.options.silent) {
|
1046
|
-
return this.loadUrl();
|
1047
|
-
}
|
1048
|
-
},
|
1049
|
-
|
1050
|
-
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
|
1051
|
-
// but possibly useful for unit testing Routers.
|
1052
|
-
stop: function() {
|
1053
|
-
$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
|
1054
|
-
clearInterval(this._checkUrlInterval);
|
1055
|
-
History.started = false;
|
1056
|
-
},
|
1057
|
-
|
1058
|
-
// Add a route to be tested when the fragment changes. Routes added later
|
1059
|
-
// may override previous routes.
|
1060
|
-
route: function(route, callback) {
|
1061
|
-
this.handlers.unshift({route: route, callback: callback});
|
1062
|
-
},
|
1063
|
-
|
1064
|
-
// Checks the current URL to see if it has changed, and if it has,
|
1065
|
-
// calls `loadUrl`, normalizing across the hidden iframe.
|
1066
|
-
checkUrl: function(e) {
|
1067
|
-
var current = this.getFragment();
|
1068
|
-
if (current == this.fragment && this.iframe) current = this.getFragment(this.getHash(this.iframe));
|
1069
|
-
if (current == this.fragment) return false;
|
1070
|
-
if (this.iframe) this.navigate(current);
|
1071
|
-
this.loadUrl() || this.loadUrl(this.getHash());
|
1072
|
-
},
|
1073
|
-
|
1074
|
-
// Attempt to load the current URL fragment. If a route succeeds with a
|
1075
|
-
// match, returns `true`. If no defined routes matches the fragment,
|
1076
|
-
// returns `false`.
|
1077
|
-
loadUrl: function(fragmentOverride) {
|
1078
|
-
var fragment = this.fragment = this.getFragment(fragmentOverride);
|
1079
|
-
var matched = _.any(this.handlers, function(handler) {
|
1080
|
-
if (handler.route.test(fragment)) {
|
1081
|
-
handler.callback(fragment);
|
1082
|
-
return true;
|
1083
|
-
}
|
1084
|
-
});
|
1085
|
-
return matched;
|
1086
|
-
},
|
1087
|
-
|
1088
|
-
// Save a fragment into the hash history, or replace the URL state if the
|
1089
|
-
// 'replace' option is passed. You are responsible for properly URL-encoding
|
1090
|
-
// the fragment in advance.
|
1091
|
-
//
|
1092
|
-
// The options object can contain `trigger: true` if you wish to have the
|
1093
|
-
// route callback be fired (not usually desirable), or `replace: true`, if
|
1094
|
-
// you wish to modify the current URL without adding an entry to the history.
|
1095
|
-
navigate: function(fragment, options) {
|
1096
|
-
if (!History.started) return false;
|
1097
|
-
if (!options || options === true) options = {trigger: options};
|
1098
|
-
var frag = (fragment || '').replace(routeStripper, '');
|
1099
|
-
if (this.fragment == frag) return;
|
1100
|
-
|
1101
|
-
// If pushState is available, we use it to set the fragment as a real URL.
|
1102
|
-
if (this._hasPushState) {
|
1103
|
-
if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
|
1104
|
-
this.fragment = frag;
|
1105
|
-
window.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, frag);
|
1106
|
-
|
1107
|
-
// If hash changes haven't been explicitly disabled, update the hash
|
1108
|
-
// fragment to store history.
|
1109
|
-
} else if (this._wantsHashChange) {
|
1110
|
-
this.fragment = frag;
|
1111
|
-
this._updateHash(window.location, frag, options.replace);
|
1112
|
-
if (this.iframe && (frag != this.getFragment(this.getHash(this.iframe)))) {
|
1113
|
-
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
|
1114
|
-
// When replace is true, we don't want this.
|
1115
|
-
if(!options.replace) this.iframe.document.open().close();
|
1116
|
-
this._updateHash(this.iframe.location, frag, options.replace);
|
1117
|
-
}
|
1118
|
-
|
1119
|
-
// If you've told us that you explicitly don't want fallback hashchange-
|
1120
|
-
// based history, then `navigate` becomes a page refresh.
|
1121
|
-
} else {
|
1122
|
-
window.location.assign(this.options.root + fragment);
|
1123
|
-
}
|
1124
|
-
if (options.trigger) this.loadUrl(fragment);
|
1125
|
-
},
|
1126
|
-
|
1127
|
-
// Update the hash location, either replacing the current entry, or adding
|
1128
|
-
// a new one to the browser history.
|
1129
|
-
_updateHash: function(location, fragment, replace) {
|
1130
|
-
if (replace) {
|
1131
|
-
location.replace(location.toString().replace(/(javascript:|#).*$/, '') + '#' + fragment);
|
1132
|
-
} else {
|
1133
|
-
location.hash = fragment;
|
1134
|
-
}
|
1135
|
-
}
|
1136
|
-
});
|
1137
|
-
|
1138
|
-
// Backbone.View
|
1139
|
-
// -------------
|
1140
|
-
|
1141
|
-
// Creating a Backbone.View creates its initial element outside of the DOM,
|
1142
|
-
// if an existing element is not provided...
|
1143
|
-
var View = Backbone.View = function(options) {
|
1144
|
-
this.cid = _.uniqueId('view');
|
1145
|
-
this._configure(options || {});
|
1146
|
-
this._ensureElement();
|
1147
|
-
this.initialize.apply(this, arguments);
|
1148
|
-
this.delegateEvents();
|
1149
|
-
};
|
1150
|
-
|
1151
|
-
// Cached regex to split keys for `delegate`.
|
1152
|
-
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
|
1153
|
-
|
1154
|
-
// List of view options to be merged as properties.
|
1155
|
-
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
|
1156
|
-
|
1157
|
-
// Set up all inheritable **Backbone.View** properties and methods.
|
1158
|
-
_.extend(View.prototype, Events, {
|
1159
|
-
|
1160
|
-
// The default `tagName` of a View's element is `"div"`.
|
1161
|
-
tagName: 'div',
|
1162
|
-
|
1163
|
-
// jQuery delegate for element lookup, scoped to DOM elements within the
|
1164
|
-
// current view. This should be prefered to global lookups where possible.
|
1165
|
-
$: function(selector) {
|
1166
|
-
return this.$el.find(selector);
|
1167
|
-
},
|
1168
|
-
|
1169
|
-
// Initialize is an empty function by default. Override it with your own
|
1170
|
-
// initialization logic.
|
1171
|
-
initialize: function(){},
|
1172
|
-
|
1173
|
-
// **render** is the core function that your view should override, in order
|
1174
|
-
// to populate its element (`this.el`), with the appropriate HTML. The
|
1175
|
-
// convention is for **render** to always return `this`.
|
1176
|
-
render: function() {
|
1177
|
-
return this;
|
1178
|
-
},
|
1179
|
-
|
1180
|
-
// Remove this view from the DOM. Note that the view isn't present in the
|
1181
|
-
// DOM by default, so calling this method may be a no-op.
|
1182
|
-
remove: function() {
|
1183
|
-
this.$el.remove();
|
1184
|
-
return this;
|
1185
|
-
},
|
1186
|
-
|
1187
|
-
// For small amounts of DOM Elements, where a full-blown template isn't
|
1188
|
-
// needed, use **make** to manufacture elements, one at a time.
|
1189
|
-
//
|
1190
|
-
// var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
|
1191
|
-
//
|
1192
|
-
make: function(tagName, attributes, content) {
|
1193
|
-
var el = document.createElement(tagName);
|
1194
|
-
if (attributes) $(el).attr(attributes);
|
1195
|
-
if (content) $(el).html(content);
|
1196
|
-
return el;
|
1197
|
-
},
|
1198
|
-
|
1199
|
-
// Change the view's element (`this.el` property), including event
|
1200
|
-
// re-delegation.
|
1201
|
-
setElement: function(element, delegate) {
|
1202
|
-
if (this.$el) this.undelegateEvents();
|
1203
|
-
this.$el = (element instanceof $) ? element : $(element);
|
1204
|
-
this.el = this.$el[0];
|
1205
|
-
if (delegate !== false) this.delegateEvents();
|
1206
|
-
return this;
|
1207
|
-
},
|
1208
|
-
|
1209
|
-
// Set callbacks, where `this.events` is a hash of
|
1210
|
-
//
|
1211
|
-
// *{"event selector": "callback"}*
|
1212
|
-
//
|
1213
|
-
// {
|
1214
|
-
// 'mousedown .title': 'edit',
|
1215
|
-
// 'click .button': 'save'
|
1216
|
-
// 'click .open': function(e) { ... }
|
1217
|
-
// }
|
1218
|
-
//
|
1219
|
-
// pairs. Callbacks will be bound to the view, with `this` set properly.
|
1220
|
-
// Uses event delegation for efficiency.
|
1221
|
-
// Omitting the selector binds the event to `this.el`.
|
1222
|
-
// This only works for delegate-able events: not `focus`, `blur`, and
|
1223
|
-
// not `change`, `submit`, and `reset` in Internet Explorer.
|
1224
|
-
delegateEvents: function(events) {
|
1225
|
-
if (!(events || (events = getValue(this, 'events')))) return;
|
1226
|
-
this.undelegateEvents();
|
1227
|
-
for (var key in events) {
|
1228
|
-
var method = events[key];
|
1229
|
-
if (!_.isFunction(method)) method = this[events[key]];
|
1230
|
-
if (!method) throw new Error('Method "' + events[key] + '" does not exist');
|
1231
|
-
var match = key.match(delegateEventSplitter);
|
1232
|
-
var eventName = match[1], selector = match[2];
|
1233
|
-
method = _.bind(method, this);
|
1234
|
-
eventName += '.delegateEvents' + this.cid;
|
1235
|
-
if (selector === '') {
|
1236
|
-
this.$el.bind(eventName, method);
|
1237
|
-
} else {
|
1238
|
-
this.$el.delegate(selector, eventName, method);
|
1239
|
-
}
|
1240
|
-
}
|
1241
|
-
},
|
1242
|
-
|
1243
|
-
// Clears all callbacks previously bound to the view with `delegateEvents`.
|
1244
|
-
// You usually don't need to use this, but may wish to if you have multiple
|
1245
|
-
// Backbone views attached to the same DOM element.
|
1246
|
-
undelegateEvents: function() {
|
1247
|
-
this.$el.unbind('.delegateEvents' + this.cid);
|
1248
|
-
},
|
1249
|
-
|
1250
|
-
// Performs the initial configuration of a View with a set of options.
|
1251
|
-
// Keys with special meaning *(model, collection, id, className)*, are
|
1252
|
-
// attached directly to the view.
|
1253
|
-
_configure: function(options) {
|
1254
|
-
if (this.options) options = _.extend({}, this.options, options);
|
1255
|
-
for (var i = 0, l = viewOptions.length; i < l; i++) {
|
1256
|
-
var attr = viewOptions[i];
|
1257
|
-
if (options[attr]) this[attr] = options[attr];
|
1258
|
-
}
|
1259
|
-
this.options = options;
|
1260
|
-
},
|
1261
|
-
|
1262
|
-
// Ensure that the View has a DOM element to render into.
|
1263
|
-
// If `this.el` is a string, pass it through `$()`, take the first
|
1264
|
-
// matching element, and re-assign it to `el`. Otherwise, create
|
1265
|
-
// an element from the `id`, `className` and `tagName` properties.
|
1266
|
-
_ensureElement: function() {
|
1267
|
-
if (!this.el) {
|
1268
|
-
var attrs = getValue(this, 'attributes') || {};
|
1269
|
-
if (this.id) attrs.id = this.id;
|
1270
|
-
if (this.className) attrs['class'] = this.className;
|
1271
|
-
this.setElement(this.make(this.tagName, attrs), false);
|
1272
|
-
} else {
|
1273
|
-
this.setElement(this.el, false);
|
1274
|
-
}
|
1275
|
-
}
|
1276
|
-
|
1277
|
-
});
|
1278
|
-
|
1279
|
-
// The self-propagating extend function that Backbone classes use.
|
1280
|
-
var extend = function (protoProps, classProps) {
|
1281
|
-
var child = inherits(this, protoProps, classProps);
|
1282
|
-
child.extend = this.extend;
|
1283
|
-
return child;
|
1284
|
-
};
|
1285
|
-
|
1286
|
-
// Set up inheritance for the model, collection, and view.
|
1287
|
-
Model.extend = Collection.extend = Router.extend = View.extend = extend;
|
1288
|
-
|
1289
|
-
// Backbone.sync
|
1290
|
-
// -------------
|
1291
|
-
|
1292
|
-
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
|
1293
|
-
var methodMap = {
|
1294
|
-
'create': 'POST',
|
1295
|
-
'update': 'PUT',
|
1296
|
-
'delete': 'DELETE',
|
1297
|
-
'read': 'GET'
|
1298
|
-
};
|
1299
|
-
|
1300
|
-
// Override this function to change the manner in which Backbone persists
|
1301
|
-
// models to the server. You will be passed the type of request, and the
|
1302
|
-
// model in question. By default, makes a RESTful Ajax request
|
1303
|
-
// to the model's `url()`. Some possible customizations could be:
|
1304
|
-
//
|
1305
|
-
// * Use `setTimeout` to batch rapid-fire updates into a single request.
|
1306
|
-
// * Send up the models as XML instead of JSON.
|
1307
|
-
// * Persist models via WebSockets instead of Ajax.
|
1308
|
-
//
|
1309
|
-
// Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
|
1310
|
-
// as `POST`, with a `_method` parameter containing the true HTTP method,
|
1311
|
-
// as well as all requests with the body as `application/x-www-form-urlencoded`
|
1312
|
-
// instead of `application/json` with the model in a param named `model`.
|
1313
|
-
// Useful when interfacing with server-side languages like **PHP** that make
|
1314
|
-
// it difficult to read the body of `PUT` requests.
|
1315
|
-
Backbone.sync = function(method, model, options) {
|
1316
|
-
var type = methodMap[method];
|
1317
|
-
|
1318
|
-
// Default options, unless specified.
|
1319
|
-
options || (options = {});
|
1320
|
-
|
1321
|
-
// Default JSON-request options.
|
1322
|
-
var params = {type: type, dataType: 'json'};
|
1323
|
-
|
1324
|
-
// Ensure that we have a URL.
|
1325
|
-
if (!options.url) {
|
1326
|
-
params.url = getValue(model, 'url') || urlError();
|
1327
|
-
}
|
1328
|
-
|
1329
|
-
// Ensure that we have the appropriate request data.
|
1330
|
-
if (!options.data && model && (method == 'create' || method == 'update')) {
|
1331
|
-
params.contentType = 'application/json';
|
1332
|
-
params.data = JSON.stringify(model.toJSON());
|
1333
|
-
}
|
1334
|
-
|
1335
|
-
// For older servers, emulate JSON by encoding the request into an HTML-form.
|
1336
|
-
if (Backbone.emulateJSON) {
|
1337
|
-
params.contentType = 'application/x-www-form-urlencoded';
|
1338
|
-
params.data = params.data ? {model: params.data} : {};
|
1339
|
-
}
|
1340
|
-
|
1341
|
-
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
|
1342
|
-
// And an `X-HTTP-Method-Override` header.
|
1343
|
-
if (Backbone.emulateHTTP) {
|
1344
|
-
if (type === 'PUT' || type === 'DELETE') {
|
1345
|
-
if (Backbone.emulateJSON) params.data._method = type;
|
1346
|
-
params.type = 'POST';
|
1347
|
-
params.beforeSend = function(xhr) {
|
1348
|
-
xhr.setRequestHeader('X-HTTP-Method-Override', type);
|
1349
|
-
};
|
1350
|
-
}
|
1351
|
-
}
|
1352
|
-
|
1353
|
-
// Don't process data on a non-GET request.
|
1354
|
-
if (params.type !== 'GET' && !Backbone.emulateJSON) {
|
1355
|
-
params.processData = false;
|
1356
|
-
}
|
1357
|
-
|
1358
|
-
// Make the request, allowing the user to override any Ajax options.
|
1359
|
-
return $.ajax(_.extend(params, options));
|
1360
|
-
};
|
1361
|
-
|
1362
|
-
// Wrap an optional error callback with a fallback error event.
|
1363
|
-
Backbone.wrapError = function(onError, originalModel, options) {
|
1364
|
-
return function(model, resp) {
|
1365
|
-
resp = model === originalModel ? resp : model;
|
1366
|
-
if (onError) {
|
1367
|
-
onError(originalModel, resp, options);
|
1368
|
-
} else {
|
1369
|
-
originalModel.trigger('error', originalModel, resp, options);
|
1370
|
-
}
|
1371
|
-
};
|
1372
|
-
};
|
1373
|
-
|
1374
|
-
// Helpers
|
1375
|
-
// -------
|
1376
|
-
|
1377
|
-
// Shared empty constructor function to aid in prototype-chain creation.
|
1378
|
-
var ctor = function(){};
|
1379
|
-
|
1380
|
-
// Helper function to correctly set up the prototype chain, for subclasses.
|
1381
|
-
// Similar to `goog.inherits`, but uses a hash of prototype properties and
|
1382
|
-
// class properties to be extended.
|
1383
|
-
var inherits = function(parent, protoProps, staticProps) {
|
1384
|
-
var child;
|
1385
|
-
|
1386
|
-
// The constructor function for the new subclass is either defined by you
|
1387
|
-
// (the "constructor" property in your `extend` definition), or defaulted
|
1388
|
-
// by us to simply call the parent's constructor.
|
1389
|
-
if (protoProps && protoProps.hasOwnProperty('constructor')) {
|
1390
|
-
child = protoProps.constructor;
|
1391
|
-
} else {
|
1392
|
-
child = function(){ parent.apply(this, arguments); };
|
1393
|
-
}
|
1394
|
-
|
1395
|
-
// Inherit class (static) properties from parent.
|
1396
|
-
_.extend(child, parent);
|
1397
|
-
|
1398
|
-
// Set the prototype chain to inherit from `parent`, without calling
|
1399
|
-
// `parent`'s constructor function.
|
1400
|
-
ctor.prototype = parent.prototype;
|
1401
|
-
child.prototype = new ctor();
|
1402
|
-
|
1403
|
-
// Add prototype properties (instance properties) to the subclass,
|
1404
|
-
// if supplied.
|
1405
|
-
if (protoProps) _.extend(child.prototype, protoProps);
|
1406
|
-
|
1407
|
-
// Add static properties to the constructor function, if supplied.
|
1408
|
-
if (staticProps) _.extend(child, staticProps);
|
1409
|
-
|
1410
|
-
// Correctly set child's `prototype.constructor`.
|
1411
|
-
child.prototype.constructor = child;
|
1412
|
-
|
1413
|
-
// Set a convenience property in case the parent's prototype is needed later.
|
1414
|
-
child.__super__ = parent.prototype;
|
1415
|
-
|
1416
|
-
return child;
|
1417
|
-
};
|
1418
|
-
|
1419
|
-
// Helper function to get a value from a Backbone object as a property
|
1420
|
-
// or as a function.
|
1421
|
-
var getValue = function(object, prop) {
|
1422
|
-
if (!(object && object[prop])) return null;
|
1423
|
-
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
|
1424
|
-
};
|
1425
|
-
|
1426
|
-
// Throw an error when a URL is needed, and none is supplied.
|
1427
|
-
var urlError = function() {
|
1428
|
-
throw new Error('A "url" property or function must be specified');
|
1429
|
-
};
|
1430
|
-
|
1431
|
-
}).call(this);
|
1432
|
-
|
1433
|
-
// Backbone.EventBinder, v0.0.0
|
1434
|
-
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
|
1435
|
-
// Distributed under MIT license
|
1436
|
-
// http://github.com/marionettejs/backbone.eventbinder
|
1437
|
-
// EventBinder
|
1438
|
-
// -----------
|
1439
|
-
//
|
1440
|
-
// The event binder facilitates the binding and unbinding of events
|
1441
|
-
// from objects that extend `Backbone.Events`. It makes
|
1442
|
-
// unbinding events, even with anonymous callback functions,
|
1443
|
-
// easy.
|
1444
|
-
//
|
1445
|
-
// Inspired by [Johnny Oshika](http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853)
|
1446
|
-
|
1447
|
-
Backbone.EventBinder = (function(Backbone, _){
|
1448
|
-
|
1449
|
-
// Constructor function
|
1450
|
-
var EventBinder = function(){
|
1451
|
-
this._eventBindings = [];
|
1452
|
-
};
|
1453
|
-
|
1454
|
-
// Copy the `extend` function used by Backbone's classes
|
1455
|
-
EventBinder.extend = Backbone.View.extend;
|
1456
|
-
|
1457
|
-
// Extend the EventBinder with additional methods
|
1458
|
-
_.extend(EventBinder.prototype, {
|
1459
|
-
// Store the event binding in array so it can be unbound
|
1460
|
-
// easily, at a later point in time.
|
1461
|
-
bindTo: function (obj, eventName, callback, context) {
|
1462
|
-
context = context || this;
|
1463
|
-
obj.on(eventName, callback, context);
|
1464
|
-
|
1465
|
-
var binding = {
|
1466
|
-
obj: obj,
|
1467
|
-
eventName: eventName,
|
1468
|
-
callback: callback,
|
1469
|
-
context: context
|
1470
|
-
};
|
1471
|
-
|
1472
|
-
this._eventBindings.push(binding);
|
1473
|
-
|
1474
|
-
return binding;
|
1475
|
-
},
|
1476
|
-
|
1477
|
-
// Unbind from a single binding object. Binding objects are
|
1478
|
-
// returned from the `bindTo` method call.
|
1479
|
-
unbindFrom: function(binding){
|
1480
|
-
binding.obj.off(binding.eventName, binding.callback, binding.context);
|
1481
|
-
this._eventBindings = _.reject(this._eventBindings, function(bind){return bind === binding;});
|
1482
|
-
},
|
1483
|
-
|
1484
|
-
// Unbind all of the events that we have stored.
|
1485
|
-
unbindAll: function () {
|
1486
|
-
var that = this;
|
1487
|
-
|
1488
|
-
// The `unbindFrom` call removes elements from the array
|
1489
|
-
// while it is being iterated, so clone it first.
|
1490
|
-
var bindings = _.map(this._eventBindings, _.identity);
|
1491
|
-
_.each(bindings, function (binding, index) {
|
1492
|
-
that.unbindFrom(binding);
|
1493
|
-
});
|
1494
|
-
}
|
1495
|
-
});
|
1496
|
-
|
1497
|
-
return EventBinder;
|
1498
|
-
})(Backbone, _);
|
1499
|
-
// Backbone.Wreqr, v0.0.0
|
1500
|
-
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
|
1501
|
-
// Distributed under MIT license
|
1502
|
-
// http://github.com/marionettejs/backbone.wreqr
|
1503
|
-
Backbone.Wreqr = (function(Backbone, Marionette, _){
|
1504
|
-
"option strict";
|
1505
|
-
var Wreqr = {};
|
1506
|
-
|
1507
|
-
Wreqr.Handlers = (function(Backbone, _){
|
1508
|
-
"option strict";
|
1509
|
-
|
1510
|
-
var Handlers = function(){
|
1511
|
-
"use strict";
|
1512
|
-
this._handlers = {};
|
1513
|
-
};
|
1514
|
-
|
1515
|
-
Handlers.extend = Backbone.Model.extend;
|
1516
|
-
|
1517
|
-
_.extend(Handlers.prototype, {
|
1518
|
-
addHandler: function(name, handler, context){
|
1519
|
-
var config = {
|
1520
|
-
callback: handler,
|
1521
|
-
context: context
|
1522
|
-
};
|
1523
|
-
|
1524
|
-
this._handlers[name] = config;
|
1525
|
-
},
|
1526
|
-
|
1527
|
-
getHandler: function(name){
|
1528
|
-
var config = this._handlers[name];
|
1529
|
-
|
1530
|
-
if (!config){
|
1531
|
-
throw new Error("Handler not found for '" + name + "'");
|
1532
|
-
}
|
1533
|
-
|
1534
|
-
return function(){
|
1535
|
-
return config.callback.apply(config.context, arguments);
|
1536
|
-
};
|
1537
|
-
},
|
1538
|
-
|
1539
|
-
removeHandler: function(name){
|
1540
|
-
delete this._handlers[name];
|
1541
|
-
},
|
1542
|
-
|
1543
|
-
removeAllHandlers: function(){
|
1544
|
-
this._handlers = {};
|
1545
|
-
}
|
1546
|
-
});
|
1547
|
-
|
1548
|
-
return Handlers;
|
1549
|
-
})(Backbone, _);
|
1550
|
-
|
1551
|
-
// Wreqr.Commands
|
1552
|
-
// --------------
|
1553
|
-
//
|
1554
|
-
// A simple command pattern implementation. Register a command
|
1555
|
-
// handler and execute it.
|
1556
|
-
Wreqr.Commands = (function(Wreqr){
|
1557
|
-
"option strict";
|
1558
|
-
|
1559
|
-
return Wreqr.Handlers.extend({
|
1560
|
-
execute: function(name, args){
|
1561
|
-
this.getHandler(name)(args);
|
1562
|
-
}
|
1563
|
-
});
|
1564
|
-
|
1565
|
-
})(Wreqr);
|
1566
|
-
|
1567
|
-
// Wreqr.RequestResponse
|
1568
|
-
// ---------------------
|
1569
|
-
//
|
1570
|
-
// A simple request/response implementation. Register a
|
1571
|
-
// request handler, and return a response from it
|
1572
|
-
Wreqr.RequestResponse = (function(Wreqr){
|
1573
|
-
"option strict";
|
1574
|
-
|
1575
|
-
return Wreqr.Handlers.extend({
|
1576
|
-
request: function(name, args){
|
1577
|
-
return this.getHandler(name)(args);
|
1578
|
-
}
|
1579
|
-
});
|
1580
|
-
|
1581
|
-
})(Wreqr);
|
1582
|
-
|
1583
|
-
// Event Aggregator
|
1584
|
-
// ----------------
|
1585
|
-
// A pub-sub object that can be used to decouple various parts
|
1586
|
-
// of an application through event-driven architecture.
|
1587
|
-
|
1588
|
-
Wreqr.EventAggregator = (function(Backbone, _){
|
1589
|
-
"option strict";
|
1590
|
-
var EA = function(){};
|
1591
|
-
|
1592
|
-
// Copy the `extend` function used by Backbone's classes
|
1593
|
-
EA.extend = Backbone.Model.extend;
|
1594
|
-
|
1595
|
-
// Copy the basic Backbone.Events on to the event aggregator
|
1596
|
-
_.extend(EA.prototype, Backbone.Events);
|
1597
|
-
|
1598
|
-
return EA;
|
1599
|
-
})(Backbone, _);
|
1600
|
-
|
1601
|
-
|
1602
|
-
return Wreqr;
|
1603
|
-
})(Backbone, Backbone.Marionette, _);
|
1604
|
-
/*!
|
1605
|
-
* Backbone.Marionette, v1.0.0-beta1
|
1606
|
-
* Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
|
1607
|
-
* Distributed under MIT license
|
1608
|
-
* http://github.com/marionettejs/backbone.marionette
|
1609
|
-
*/
|
1610
|
-
Backbone.Marionette = Marionette = (function(Backbone, _, $){
|
1611
|
-
var Marionette = {};
|
1612
|
-
|
1613
|
-
// Helpers
|
1614
|
-
// -------
|
1615
|
-
|
1616
|
-
// For slicing `arguments` in functions
|
1617
|
-
var slice = Array.prototype.slice;
|
1618
|
-
|
1619
|
-
// Borrow the Backbone `extend` method so we can use it as needed
|
1620
|
-
Marionette.extend = Backbone.Model.extend;
|
1621
|
-
|
1622
|
-
// Trigger an event and a corresponding method name. Examples:
|
1623
|
-
//
|
1624
|
-
// `this.triggerMethod("foo")` will trigger the "foo" event and
|
1625
|
-
// call the "onFoo" method.
|
1626
|
-
//
|
1627
|
-
// `this.triggerMethod("foo:bar") will trigger the "foo:bar" event and
|
1628
|
-
// call the "onFooBar" method.
|
1629
|
-
Marionette.triggerMethod = function(){
|
1630
|
-
var args = Array.prototype.slice.apply(arguments);
|
1631
|
-
var eventName = args[0];
|
1632
|
-
var segments = eventName.split(":");
|
1633
|
-
var segment, capLetter, methodName = "on";
|
1634
|
-
|
1635
|
-
for (var i = 0; i < segments.length; i++){
|
1636
|
-
segment = segments[i];
|
1637
|
-
capLetter = segment.charAt(0).toUpperCase();
|
1638
|
-
methodName += capLetter + segment.slice(1);
|
1639
|
-
}
|
1640
|
-
|
1641
|
-
this.trigger.apply(this, arguments);
|
1642
|
-
|
1643
|
-
if (_.isFunction(this[methodName])){
|
1644
|
-
args.shift();
|
1645
|
-
this[methodName].apply(this, args);
|
1646
|
-
}
|
1647
|
-
};
|
1648
|
-
|
1649
|
-
// EventBinder
|
1650
|
-
// -----------
|
1651
|
-
// Import the event binder from it's new home
|
1652
|
-
// https://github.com/marionettejs/backbone.eventbinder
|
1653
|
-
Marionette.EventBinder = Backbone.EventBinder;
|
1654
|
-
|
1655
|
-
// Add the EventBinder methods to the view directly,
|
1656
|
-
// but keep them bound to the EventBinder instance so they work properly.
|
1657
|
-
// This allows the event binder's implementation to vary independently
|
1658
|
-
// of it being attached to the view... for example the internal structure
|
1659
|
-
// used to store the events can change without worry about it interfering
|
1660
|
-
// with Marionette's views.
|
1661
|
-
Marionette.addEventBinder = function(target){
|
1662
|
-
var eventBinder = new Marionette.EventBinder();
|
1663
|
-
target.eventBinder = eventBinder;
|
1664
|
-
target.bindTo = _.bind(eventBinder.bindTo, eventBinder);
|
1665
|
-
target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
|
1666
|
-
target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
|
1667
|
-
};
|
1668
|
-
|
1669
|
-
// Marionette.View
|
1670
|
-
// ---------------
|
1671
|
-
|
1672
|
-
// The core view type that other Marionette views extend from.
|
1673
|
-
Marionette.View = Backbone.View.extend({
|
1674
|
-
|
1675
|
-
constructor: function(){
|
1676
|
-
_.bindAll(this, "render");
|
1677
|
-
Marionette.addEventBinder(this);
|
1678
|
-
|
1679
|
-
Backbone.View.prototype.constructor.apply(this, arguments);
|
1680
|
-
|
1681
|
-
this.bindBackboneEntityTo(this.model, this.modelEvents);
|
1682
|
-
this.bindBackboneEntityTo(this.collection, this.collectionEvents);
|
1683
|
-
|
1684
|
-
this.bindTo(this, "show", this.onShowCalled, this);
|
1685
|
-
},
|
1686
|
-
|
1687
|
-
// import the "triggerMethod" to trigger events with corresponding
|
1688
|
-
// methods if the method exists
|
1689
|
-
triggerMethod: Marionette.triggerMethod,
|
1690
|
-
|
1691
|
-
// Get the template for this view
|
1692
|
-
// instance. You can set a `template` attribute in the view
|
1693
|
-
// definition or pass a `template: "whatever"` parameter in
|
1694
|
-
// to the constructor options.
|
1695
|
-
getTemplate: function(){
|
1696
|
-
var template;
|
1697
|
-
|
1698
|
-
// Get the template from `this.options.template` or
|
1699
|
-
// `this.template`. The `options` takes precedence.
|
1700
|
-
if (this.options && this.options.template){
|
1701
|
-
template = this.options.template;
|
1702
|
-
} else {
|
1703
|
-
template = this.template;
|
1704
|
-
}
|
1705
|
-
|
1706
|
-
return template;
|
1707
|
-
},
|
1708
|
-
|
1709
|
-
// Mix in template helper methods. Looks for a
|
1710
|
-
// `templateHelpers` attribute, which can either be an
|
1711
|
-
// object literal, or a function that returns an object
|
1712
|
-
// literal. All methods and attributes from this object
|
1713
|
-
// are copies to the object passed in.
|
1714
|
-
mixinTemplateHelpers: function(target){
|
1715
|
-
target = target || {};
|
1716
|
-
var templateHelpers = this.templateHelpers;
|
1717
|
-
if (_.isFunction(templateHelpers)){
|
1718
|
-
templateHelpers = templateHelpers.call(this);
|
1719
|
-
}
|
1720
|
-
return _.extend(target, templateHelpers);
|
1721
|
-
},
|
1722
|
-
|
1723
|
-
// Configure `triggers` to forward DOM events to view
|
1724
|
-
// events. `triggers: {"click .foo": "do:foo"}`
|
1725
|
-
configureTriggers: function(){
|
1726
|
-
if (!this.triggers) { return; }
|
1727
|
-
|
1728
|
-
var triggers = this.triggers;
|
1729
|
-
var that = this;
|
1730
|
-
var triggerEvents = {};
|
1731
|
-
|
1732
|
-
// Allow `triggers` to be configured as a function
|
1733
|
-
if (_.isFunction(triggers)){ triggers = triggers.call(this); }
|
1734
|
-
|
1735
|
-
// Configure the triggers, prevent default
|
1736
|
-
// action and stop propagation of DOM events
|
1737
|
-
_.each(triggers, function(value, key){
|
1738
|
-
|
1739
|
-
triggerEvents[key] = function(e){
|
1740
|
-
if (e && e.preventDefault){ e.preventDefault(); }
|
1741
|
-
if (e && e.stopPropagation){ e.stopPropagation(); }
|
1742
|
-
that.trigger(value);
|
1743
|
-
};
|
1744
|
-
|
1745
|
-
});
|
1746
|
-
|
1747
|
-
return triggerEvents;
|
1748
|
-
},
|
1749
|
-
|
1750
|
-
// Overriding Backbone.View's delegateEvents specifically
|
1751
|
-
// to handle the `triggers` configuration
|
1752
|
-
delegateEvents: function(events){
|
1753
|
-
events = events || this.events;
|
1754
|
-
if (_.isFunction(events)){ events = events.call(this); }
|
1755
|
-
|
1756
|
-
var combinedEvents = {};
|
1757
|
-
var triggers = this.configureTriggers();
|
1758
|
-
_.extend(combinedEvents, events, triggers);
|
1759
|
-
|
1760
|
-
Backbone.View.prototype.delegateEvents.call(this, combinedEvents);
|
1761
|
-
},
|
1762
|
-
|
1763
|
-
// Internal method, handles the `show` event.
|
1764
|
-
onShowCalled: function(){},
|
1765
|
-
|
1766
|
-
// Default `close` implementation, for removing a view from the
|
1767
|
-
// DOM and unbinding it. Regions will call this method
|
1768
|
-
// for you. You can specify an `onClose` method in your view to
|
1769
|
-
// add custom code that is called after the view is closed.
|
1770
|
-
close: function(){
|
1771
|
-
if (this.isClosed) { return; }
|
1772
|
-
|
1773
|
-
this.triggerMethod("before:close");
|
1774
|
-
|
1775
|
-
this.remove();
|
1776
|
-
this.unbindAll();
|
1777
|
-
|
1778
|
-
this.triggerMethod("close");
|
1779
|
-
this.isClosed = true;
|
1780
|
-
},
|
1781
|
-
|
1782
|
-
// This method binds the elements specified in the "ui" hash inside the view's code with
|
1783
|
-
// the associated jQuery selectors.
|
1784
|
-
bindUIElements: function(){
|
1785
|
-
if (!this.ui) { return; }
|
1786
|
-
|
1787
|
-
var that = this;
|
1788
|
-
|
1789
|
-
if (!this.uiBindings) {
|
1790
|
-
// We want to store the ui hash in uiBindings, since afterwards the values in the ui hash
|
1791
|
-
// will be overridden with jQuery selectors.
|
1792
|
-
this.uiBindings = this.ui;
|
1793
|
-
}
|
1794
|
-
|
1795
|
-
// refreshing the associated selectors since they should point to the newly rendered elements.
|
1796
|
-
this.ui = {};
|
1797
|
-
_.each(_.keys(this.uiBindings), function(key) {
|
1798
|
-
var selector = that.uiBindings[key];
|
1799
|
-
that.ui[key] = that.$(selector);
|
1800
|
-
});
|
1801
|
-
},
|
1802
|
-
|
1803
|
-
// This method is used to bind a backbone "entity" (collection/model) to methods on the view.
|
1804
|
-
bindBackboneEntityTo: function(entity, bindings){
|
1805
|
-
if (!entity || !bindings) { return; }
|
1806
|
-
|
1807
|
-
var view = this;
|
1808
|
-
_.each(bindings, function(methodName, evt){
|
1809
|
-
|
1810
|
-
var method = view[methodName];
|
1811
|
-
if(!method) {
|
1812
|
-
throw new Error("View method '"+ methodName +"' was configured as an event handler, but does not exist.");
|
1813
|
-
}
|
1814
|
-
|
1815
|
-
view.bindTo(entity, evt, method, view);
|
1816
|
-
});
|
1817
|
-
}
|
1818
|
-
});
|
1819
|
-
|
1820
|
-
// Item View
|
1821
|
-
// ---------
|
1822
|
-
|
1823
|
-
// A single item view implementation that contains code for rendering
|
1824
|
-
// with underscore.js templates, serializing the view's model or collection,
|
1825
|
-
// and calling several methods on extended views, such as `onRender`.
|
1826
|
-
Marionette.ItemView = Marionette.View.extend({
|
1827
|
-
constructor: function(){
|
1828
|
-
Marionette.View.prototype.constructor.apply(this, arguments);
|
1829
|
-
|
1830
|
-
if (this.initialEvents){
|
1831
|
-
this.initialEvents();
|
1832
|
-
}
|
1833
|
-
},
|
1834
|
-
|
1835
|
-
// Serialize the model or collection for the view. If a model is
|
1836
|
-
// found, `.toJSON()` is called. If a collection is found, `.toJSON()`
|
1837
|
-
// is also called, but is used to populate an `items` array in the
|
1838
|
-
// resulting data. If both are found, defaults to the model.
|
1839
|
-
// You can override the `serializeData` method in your own view
|
1840
|
-
// definition, to provide custom serialization for your view's data.
|
1841
|
-
serializeData: function(){
|
1842
|
-
var data;
|
1843
|
-
|
1844
|
-
if (this.model) {
|
1845
|
-
data = this.model.toJSON();
|
1846
|
-
}
|
1847
|
-
else if (this.collection) {
|
1848
|
-
data = { items: this.collection.toJSON() };
|
1849
|
-
}
|
1850
|
-
|
1851
|
-
data = this.mixinTemplateHelpers(data);
|
1852
|
-
|
1853
|
-
return data;
|
1854
|
-
},
|
1855
|
-
|
1856
|
-
// Render the view, defaulting to underscore.js templates.
|
1857
|
-
// You can override this in your view definition to provide
|
1858
|
-
// a very specific rendering for your view. In general, though,
|
1859
|
-
// you should override the `Marionette.Renderer` object to
|
1860
|
-
// change how Marionette renders views.
|
1861
|
-
render: function(){
|
1862
|
-
this.isClosed = false;
|
1863
|
-
|
1864
|
-
this.triggerMethod("before:render", this);
|
1865
|
-
this.triggerMethod("item:before:render", this);
|
1866
|
-
|
1867
|
-
var data = this.serializeData();
|
1868
|
-
var template = this.getTemplate();
|
1869
|
-
var html = Marionette.Renderer.render(template, data);
|
1870
|
-
this.$el.html(html);
|
1871
|
-
this.bindUIElements();
|
1872
|
-
|
1873
|
-
this.triggerMethod("render", this);
|
1874
|
-
this.triggerMethod("item:rendered", this);
|
1875
|
-
|
1876
|
-
return this;
|
1877
|
-
},
|
1878
|
-
|
1879
|
-
// Override the default close event to add a few
|
1880
|
-
// more events that are triggered.
|
1881
|
-
close: function(){
|
1882
|
-
if (this.isClosed){ return; }
|
1883
|
-
|
1884
|
-
this.triggerMethod('item:before:close');
|
1885
|
-
Marionette.View.prototype.close.apply(this, arguments);
|
1886
|
-
this.triggerMethod('item:closed');
|
1887
|
-
}
|
1888
|
-
});
|
1889
|
-
|
1890
|
-
// Collection View
|
1891
|
-
// ---------------
|
1892
|
-
|
1893
|
-
// A view that iterates over a Backbone.Collection
|
1894
|
-
// and renders an individual ItemView for each model.
|
1895
|
-
Marionette.CollectionView = Marionette.View.extend({
|
1896
|
-
constructor: function(){
|
1897
|
-
this.initChildViewStorage();
|
1898
|
-
Marionette.View.prototype.constructor.apply(this, arguments);
|
1899
|
-
this.initialEvents();
|
1900
|
-
this.onShowCallbacks = new Marionette.Callbacks();
|
1901
|
-
},
|
1902
|
-
|
1903
|
-
// Configured the initial events that the collection view
|
1904
|
-
// binds to. Override this method to prevent the initial
|
1905
|
-
// events, or to add your own initial events.
|
1906
|
-
initialEvents: function(){
|
1907
|
-
if (this.collection){
|
1908
|
-
this.bindTo(this.collection, "add", this.addChildView, this);
|
1909
|
-
this.bindTo(this.collection, "remove", this.removeItemView, this);
|
1910
|
-
this.bindTo(this.collection, "reset", this.render, this);
|
1911
|
-
}
|
1912
|
-
},
|
1913
|
-
|
1914
|
-
// Handle a child item added to the collection
|
1915
|
-
addChildView: function(item, collection, options){
|
1916
|
-
this.closeEmptyView();
|
1917
|
-
var ItemView = this.getItemView(item);
|
1918
|
-
|
1919
|
-
var index;
|
1920
|
-
if(options && options.index){
|
1921
|
-
index = options.index;
|
1922
|
-
} else {
|
1923
|
-
index = 0;
|
1924
|
-
}
|
1925
|
-
|
1926
|
-
return this.addItemView(item, ItemView, index);
|
1927
|
-
},
|
1928
|
-
|
1929
|
-
// Override from `Marionette.View` to guarantee the `onShow` method
|
1930
|
-
// of child views is called.
|
1931
|
-
onShowCalled: function(){
|
1932
|
-
this.onShowCallbacks.run();
|
1933
|
-
},
|
1934
|
-
|
1935
|
-
// Internal method to trigger the before render callbacks
|
1936
|
-
// and events
|
1937
|
-
triggerBeforeRender: function(){
|
1938
|
-
this.triggerMethod("before:render", this);
|
1939
|
-
this.triggerMethod("collection:before:render", this);
|
1940
|
-
},
|
1941
|
-
|
1942
|
-
// Internal method to trigger the rendered callbacks and
|
1943
|
-
// events
|
1944
|
-
triggerRendered: function(){
|
1945
|
-
this.triggerMethod("render", this);
|
1946
|
-
this.triggerMethod("collection:rendered", this);
|
1947
|
-
},
|
1948
|
-
|
1949
|
-
// Render the collection of items. Override this method to
|
1950
|
-
// provide your own implementation of a render function for
|
1951
|
-
// the collection view.
|
1952
|
-
render: function(){
|
1953
|
-
this.isClosed = false;
|
1954
|
-
|
1955
|
-
this.triggerBeforeRender();
|
1956
|
-
this.closeEmptyView();
|
1957
|
-
this.closeChildren();
|
1958
|
-
|
1959
|
-
if (this.collection && this.collection.length > 0) {
|
1960
|
-
this.showCollection();
|
1961
|
-
} else {
|
1962
|
-
this.showEmptyView();
|
1963
|
-
}
|
1964
|
-
|
1965
|
-
this.triggerRendered();
|
1966
|
-
return this;
|
1967
|
-
},
|
1968
|
-
|
1969
|
-
// Internal method to loop through each item in the
|
1970
|
-
// collection view and show it
|
1971
|
-
showCollection: function(){
|
1972
|
-
var that = this;
|
1973
|
-
var ItemView;
|
1974
|
-
this.collection.each(function(item, index){
|
1975
|
-
ItemView = that.getItemView(item);
|
1976
|
-
that.addItemView(item, ItemView, index);
|
1977
|
-
});
|
1978
|
-
},
|
1979
|
-
|
1980
|
-
// Internal method to show an empty view in place of
|
1981
|
-
// a collection of item views, when the collection is
|
1982
|
-
// empty
|
1983
|
-
showEmptyView: function(){
|
1984
|
-
var EmptyView = this.options.emptyView || this.emptyView;
|
1985
|
-
if (EmptyView && !this._showingEmptyView){
|
1986
|
-
this._showingEmptyView = true;
|
1987
|
-
var model = new Backbone.Model();
|
1988
|
-
this.addItemView(model, EmptyView, 0);
|
1989
|
-
}
|
1990
|
-
},
|
1991
|
-
|
1992
|
-
// Internal method to close an existing emptyView instance
|
1993
|
-
// if one exists. Called when a collection view has been
|
1994
|
-
// rendered empty, and then an item is added to the collection.
|
1995
|
-
closeEmptyView: function(){
|
1996
|
-
if (this._showingEmptyView){
|
1997
|
-
this.closeChildren();
|
1998
|
-
delete this._showingEmptyView;
|
1999
|
-
}
|
2000
|
-
},
|
2001
|
-
|
2002
|
-
// Retrieve the itemView type, either from `this.options.itemView`
|
2003
|
-
// or from the `itemView` in the object definition. The "options"
|
2004
|
-
// takes precedence.
|
2005
|
-
getItemView: function(item){
|
2006
|
-
var itemView = this.options.itemView || this.itemView;
|
2007
|
-
|
2008
|
-
if (!itemView){
|
2009
|
-
var err = new Error("An `itemView` must be specified");
|
2010
|
-
err.name = "NoItemViewError";
|
2011
|
-
throw err;
|
2012
|
-
}
|
2013
|
-
|
2014
|
-
return itemView;
|
2015
|
-
},
|
2016
|
-
|
2017
|
-
// Render the child item's view and add it to the
|
2018
|
-
// HTML for the collection view.
|
2019
|
-
addItemView: function(item, ItemView, index){
|
2020
|
-
var that = this;
|
2021
|
-
|
2022
|
-
var view = this.buildItemView(item, ItemView);
|
2023
|
-
|
2024
|
-
// Store the child view itself so we can properly
|
2025
|
-
// remove and/or close it later
|
2026
|
-
this.storeChild(view);
|
2027
|
-
this.triggerMethod("item:added", view);
|
2028
|
-
|
2029
|
-
// Render it and show it
|
2030
|
-
var renderResult = this.renderItemView(view, index);
|
2031
|
-
|
2032
|
-
// call onShow for child item views
|
2033
|
-
if (view.onShow){
|
2034
|
-
this.onShowCallbacks.add(view.onShow, view);
|
2035
|
-
}
|
2036
|
-
|
2037
|
-
// Forward all child item view events through the parent,
|
2038
|
-
// prepending "itemview:" to the event name
|
2039
|
-
var childBinding = this.bindTo(view, "all", function(){
|
2040
|
-
var args = slice.call(arguments);
|
2041
|
-
args[0] = "itemview:" + args[0];
|
2042
|
-
args.splice(1, 0, view);
|
2043
|
-
|
2044
|
-
that.triggerMethod.apply(that, args);
|
2045
|
-
});
|
2046
|
-
|
2047
|
-
// Store all child event bindings so we can unbind
|
2048
|
-
// them when removing / closing the child view
|
2049
|
-
this.childBindings = this.childBindings || {};
|
2050
|
-
this.childBindings[view.cid] = childBinding;
|
2051
|
-
|
2052
|
-
return renderResult;
|
2053
|
-
},
|
2054
|
-
|
2055
|
-
// render the item view
|
2056
|
-
renderItemView: function(view, index) {
|
2057
|
-
view.render();
|
2058
|
-
this.appendHtml(this, view, index);
|
2059
|
-
},
|
2060
|
-
|
2061
|
-
// Build an `itemView` for every model in the collection.
|
2062
|
-
buildItemView: function(item, ItemView){
|
2063
|
-
var itemViewOptions;
|
2064
|
-
|
2065
|
-
if (_.isFunction(this.itemViewOptions)){
|
2066
|
-
itemViewOptions = this.itemViewOptions(item);
|
2067
|
-
} else {
|
2068
|
-
itemViewOptions = this.itemViewOptions;
|
2069
|
-
}
|
2070
|
-
|
2071
|
-
var options = _.extend({model: item}, itemViewOptions);
|
2072
|
-
var view = new ItemView(options);
|
2073
|
-
return view;
|
2074
|
-
},
|
2075
|
-
|
2076
|
-
// Remove the child view and close it
|
2077
|
-
removeItemView: function(item){
|
2078
|
-
var view = this.children[item.cid];
|
2079
|
-
if (view){
|
2080
|
-
var childBinding = this.childBindings[view.cid];
|
2081
|
-
if (childBinding) {
|
2082
|
-
this.unbindFrom(childBinding);
|
2083
|
-
delete this.childBindings[view.cid];
|
2084
|
-
}
|
2085
|
-
view.close();
|
2086
|
-
delete this.children[item.cid];
|
2087
|
-
}
|
2088
|
-
|
2089
|
-
if (!this.collection || this.collection.length === 0){
|
2090
|
-
this.showEmptyView();
|
2091
|
-
}
|
2092
|
-
|
2093
|
-
this.triggerMethod("item:removed", view);
|
2094
|
-
},
|
2095
|
-
|
2096
|
-
// Append the HTML to the collection's `el`.
|
2097
|
-
// Override this method to do something other
|
2098
|
-
// then `.append`.
|
2099
|
-
appendHtml: function(collectionView, itemView, index){
|
2100
|
-
collectionView.$el.append(itemView.el);
|
2101
|
-
},
|
2102
|
-
|
2103
|
-
// Store references to all of the child `itemView`
|
2104
|
-
// instances so they can be managed and cleaned up, later.
|
2105
|
-
storeChild: function(view){
|
2106
|
-
this.children[view.model.cid] = view;
|
2107
|
-
},
|
2108
|
-
|
2109
|
-
// Internal method to set up the `children` object for
|
2110
|
-
// storing all of the child views
|
2111
|
-
initChildViewStorage: function(){
|
2112
|
-
this.children = {};
|
2113
|
-
},
|
2114
|
-
|
2115
|
-
// Handle cleanup and other closing needs for
|
2116
|
-
// the collection of views.
|
2117
|
-
close: function(){
|
2118
|
-
if (this.isClosed){ return; }
|
2119
|
-
|
2120
|
-
this.triggerMethod("collection:before:close");
|
2121
|
-
this.closeChildren();
|
2122
|
-
this.triggerMethod("collection:closed");
|
2123
|
-
Marionette.View.prototype.close.apply(this, arguments);
|
2124
|
-
},
|
2125
|
-
|
2126
|
-
// Close the child views that this collection view
|
2127
|
-
// is holding on to, if any
|
2128
|
-
closeChildren: function(){
|
2129
|
-
var that = this;
|
2130
|
-
if (this.children){
|
2131
|
-
_.each(_.clone(this.children), function(childView){
|
2132
|
-
that.removeItemView(childView.model);
|
2133
|
-
});
|
2134
|
-
}
|
2135
|
-
}
|
2136
|
-
});
|
2137
|
-
|
2138
|
-
|
2139
|
-
// Composite View
|
2140
|
-
// --------------
|
2141
|
-
|
2142
|
-
// Used for rendering a branch-leaf, hierarchical structure.
|
2143
|
-
// Extends directly from CollectionView and also renders an
|
2144
|
-
// an item view as `modelView`, for the top leaf
|
2145
|
-
Marionette.CompositeView = Marionette.CollectionView.extend({
|
2146
|
-
constructor: function(options){
|
2147
|
-
Marionette.CollectionView.apply(this, arguments);
|
2148
|
-
this.itemView = this.getItemView();
|
2149
|
-
},
|
2150
|
-
|
2151
|
-
// Configured the initial events that the composite view
|
2152
|
-
// binds to. Override this method to prevent the initial
|
2153
|
-
// events, or to add your own initial events.
|
2154
|
-
initialEvents: function(){
|
2155
|
-
if (this.collection){
|
2156
|
-
this.bindTo(this.collection, "add", this.addChildView, this);
|
2157
|
-
this.bindTo(this.collection, "remove", this.removeItemView, this);
|
2158
|
-
this.bindTo(this.collection, "reset", this.renderCollection, this);
|
2159
|
-
}
|
2160
|
-
},
|
2161
|
-
|
2162
|
-
// Retrieve the `itemView` to be used when rendering each of
|
2163
|
-
// the items in the collection. The default is to return
|
2164
|
-
// `this.itemView` or Marionette.CompositeView if no `itemView`
|
2165
|
-
// has been defined
|
2166
|
-
getItemView: function(item){
|
2167
|
-
var itemView = this.options.itemView || this.itemView || this.constructor;
|
2168
|
-
|
2169
|
-
if (!itemView){
|
2170
|
-
var err = new Error("An `itemView` must be specified");
|
2171
|
-
err.name = "NoItemViewError";
|
2172
|
-
throw err;
|
2173
|
-
}
|
2174
|
-
|
2175
|
-
return itemView;
|
2176
|
-
},
|
2177
|
-
|
2178
|
-
// Serialize the collection for the view.
|
2179
|
-
// You can override the `serializeData` method in your own view
|
2180
|
-
// definition, to provide custom serialization for your view's data.
|
2181
|
-
serializeData: function(){
|
2182
|
-
var data = {};
|
2183
|
-
|
2184
|
-
if (this.model){
|
2185
|
-
data = this.model.toJSON();
|
2186
|
-
}
|
2187
|
-
|
2188
|
-
data = this.mixinTemplateHelpers(data);
|
2189
|
-
|
2190
|
-
return data;
|
2191
|
-
},
|
2192
|
-
|
2193
|
-
// Renders the model once, and the collection once. Calling
|
2194
|
-
// this again will tell the model's view to re-render itself
|
2195
|
-
// but the collection will not re-render.
|
2196
|
-
render: function(){
|
2197
|
-
this.isClosed = false;
|
2198
|
-
|
2199
|
-
this.resetItemViewContainer();
|
2200
|
-
|
2201
|
-
var html = this.renderModel();
|
2202
|
-
this.$el.html(html);
|
2203
|
-
|
2204
|
-
// the ui bindings is done here and not at the end of render since they
|
2205
|
-
// will not be available until after the model is rendered, but should be
|
2206
|
-
// available before the collection is rendered.
|
2207
|
-
this.bindUIElements();
|
2208
|
-
|
2209
|
-
this.triggerMethod("composite:model:rendered");
|
2210
|
-
this.triggerMethod("render");
|
2211
|
-
|
2212
|
-
this.renderCollection();
|
2213
|
-
this.triggerMethod("composite:rendered");
|
2214
|
-
return this;
|
2215
|
-
},
|
2216
|
-
|
2217
|
-
// Render the collection for the composite view
|
2218
|
-
renderCollection: function(){
|
2219
|
-
Marionette.CollectionView.prototype.render.apply(this, arguments);
|
2220
|
-
this.triggerMethod("composite:collection:rendered");
|
2221
|
-
},
|
2222
|
-
|
2223
|
-
// Render an individual model, if we have one, as
|
2224
|
-
// part of a composite view (branch / leaf). For example:
|
2225
|
-
// a treeview.
|
2226
|
-
renderModel: function(){
|
2227
|
-
var data = {};
|
2228
|
-
data = this.serializeData();
|
2229
|
-
|
2230
|
-
var template = this.getTemplate();
|
2231
|
-
return Marionette.Renderer.render(template, data);
|
2232
|
-
},
|
2233
|
-
|
2234
|
-
// Appends the `el` of itemView instances to the specified
|
2235
|
-
// `itemViewContainer` (a jQuery selector). Override this method to
|
2236
|
-
// provide custom logic of how the child item view instances have their
|
2237
|
-
// HTML appended to the composite view instance.
|
2238
|
-
appendHtml: function(cv, iv){
|
2239
|
-
var $container = this.getItemViewContainer(cv);
|
2240
|
-
$container.append(iv.el);
|
2241
|
-
},
|
2242
|
-
|
2243
|
-
// Internal method to ensure an `$itemViewContainer` exists, for the
|
2244
|
-
// `appendHtml` method to use.
|
2245
|
-
getItemViewContainer: function(containerView){
|
2246
|
-
if ("$itemViewContainer" in containerView){
|
2247
|
-
return containerView.$itemViewContainer;
|
2248
|
-
}
|
2249
|
-
|
2250
|
-
var container;
|
2251
|
-
if (containerView.itemViewContainer){
|
2252
|
-
|
2253
|
-
var selector = _.result(containerView, "itemViewContainer");
|
2254
|
-
container = containerView.$(selector);
|
2255
|
-
if (container.length <= 0) {
|
2256
|
-
var err = new Error("The specified `itemViewContainer` was not found: " + containerView.itemViewContainer);
|
2257
|
-
err.name = "ItemViewContainerMissingError";
|
2258
|
-
throw err;
|
2259
|
-
}
|
2260
|
-
|
2261
|
-
} else {
|
2262
|
-
container = containerView.$el;
|
2263
|
-
}
|
2264
|
-
|
2265
|
-
containerView.$itemViewContainer = container;
|
2266
|
-
return container;
|
2267
|
-
},
|
2268
|
-
|
2269
|
-
// Internal method to reset the `$itemViewContainer` on render
|
2270
|
-
resetItemViewContainer: function(){
|
2271
|
-
if (this.$itemViewContainer){
|
2272
|
-
delete this.$itemViewContainer;
|
2273
|
-
}
|
2274
|
-
}
|
2275
|
-
});
|
2276
|
-
|
2277
|
-
|
2278
|
-
// Region
|
2279
|
-
// ------
|
2280
|
-
//
|
2281
|
-
// Manage the visual regions of your composite application. See
|
2282
|
-
// http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/
|
2283
|
-
|
2284
|
-
Marionette.Region = function(options){
|
2285
|
-
this.options = options || {};
|
2286
|
-
|
2287
|
-
var el = this.options.el;
|
2288
|
-
delete this.options.el;
|
2289
|
-
|
2290
|
-
Marionette.addEventBinder(this);
|
2291
|
-
|
2292
|
-
if (el){
|
2293
|
-
this.el = el;
|
2294
|
-
}
|
2295
|
-
|
2296
|
-
if (!this.el){
|
2297
|
-
var err = new Error("An 'el' must be specified for a region.");
|
2298
|
-
err.name = "NoElError";
|
2299
|
-
throw err;
|
2300
|
-
}
|
2301
|
-
|
2302
|
-
if (this.initialize){
|
2303
|
-
this.initialize.apply(this, arguments);
|
2304
|
-
}
|
2305
|
-
};
|
2306
|
-
|
2307
|
-
|
2308
|
-
// Region Type methods
|
2309
|
-
// -------------------
|
2310
|
-
|
2311
|
-
_.extend(Marionette.Region, {
|
2312
|
-
|
2313
|
-
// Build an instance of a region by passing in a configuration object
|
2314
|
-
// and a default region type to use if none is specified in the config.
|
2315
|
-
//
|
2316
|
-
// The config object should either be a string as a jQuery DOM selector,
|
2317
|
-
// a Region type directly, or an object literal that specifies both
|
2318
|
-
// a selector and regionType:
|
2319
|
-
//
|
2320
|
-
// ```js
|
2321
|
-
// {
|
2322
|
-
// selector: "#foo",
|
2323
|
-
// regionType: MyCustomRegion
|
2324
|
-
// }
|
2325
|
-
// ```
|
2326
|
-
//
|
2327
|
-
buildRegion: function(regionConfig, defaultRegionType){
|
2328
|
-
var regionIsString = (typeof regionConfig === "string");
|
2329
|
-
var regionSelectorIsString = (typeof regionConfig.selector === "string");
|
2330
|
-
var regionTypeIsUndefined = (typeof regionConfig.regionType === "undefined");
|
2331
|
-
var regionIsType = (typeof regionConfig === "function");
|
2332
|
-
|
2333
|
-
if (!regionIsType && !regionIsString && !regionSelectorIsString) {
|
2334
|
-
throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");
|
2335
|
-
}
|
2336
|
-
|
2337
|
-
var selector, RegionType;
|
2338
|
-
|
2339
|
-
// get the selector for the region
|
2340
|
-
|
2341
|
-
if (regionIsString) {
|
2342
|
-
selector = regionConfig;
|
2343
|
-
}
|
2344
|
-
|
2345
|
-
if (regionConfig.selector) {
|
2346
|
-
selector = regionConfig.selector;
|
2347
|
-
}
|
2348
|
-
|
2349
|
-
// get the type for the region
|
2350
|
-
|
2351
|
-
if (regionIsType){
|
2352
|
-
RegionType = regionConfig;
|
2353
|
-
}
|
2354
|
-
|
2355
|
-
if (!regionIsType && regionTypeIsUndefined) {
|
2356
|
-
RegionType = defaultRegionType;
|
2357
|
-
}
|
2358
|
-
|
2359
|
-
if (regionConfig.regionType) {
|
2360
|
-
RegionType = regionConfig.regionType;
|
2361
|
-
}
|
2362
|
-
|
2363
|
-
// build the region instance
|
2364
|
-
|
2365
|
-
var regionManager = new RegionType({
|
2366
|
-
el: selector
|
2367
|
-
});
|
2368
|
-
|
2369
|
-
return regionManager;
|
2370
|
-
}
|
2371
|
-
|
2372
|
-
});
|
2373
|
-
|
2374
|
-
// Region Instance Methods
|
2375
|
-
// -----------------------
|
2376
|
-
|
2377
|
-
_.extend(Marionette.Region.prototype, Backbone.Events, {
|
2378
|
-
|
2379
|
-
// Displays a backbone view instance inside of the region.
|
2380
|
-
// Handles calling the `render` method for you. Reads content
|
2381
|
-
// directly from the `el` attribute. Also calls an optional
|
2382
|
-
// `onShow` and `close` method on your view, just after showing
|
2383
|
-
// or just before closing the view, respectively.
|
2384
|
-
show: function(view){
|
2385
|
-
|
2386
|
-
this.ensureEl();
|
2387
|
-
this.close();
|
2388
|
-
|
2389
|
-
view.render();
|
2390
|
-
this.open(view);
|
2391
|
-
|
2392
|
-
if (view.onShow) { view.onShow(); }
|
2393
|
-
view.trigger("show");
|
2394
|
-
|
2395
|
-
if (this.onShow) { this.onShow(view); }
|
2396
|
-
this.trigger("view:show", view);
|
2397
|
-
|
2398
|
-
this.currentView = view;
|
2399
|
-
},
|
2400
|
-
|
2401
|
-
ensureEl: function(){
|
2402
|
-
if (!this.$el || this.$el.length === 0){
|
2403
|
-
this.$el = this.getEl(this.el);
|
2404
|
-
}
|
2405
|
-
},
|
2406
|
-
|
2407
|
-
// Override this method to change how the region finds the
|
2408
|
-
// DOM element that it manages. Return a jQuery selector object.
|
2409
|
-
getEl: function(selector){
|
2410
|
-
return $(selector);
|
2411
|
-
},
|
2412
|
-
|
2413
|
-
// Override this method to change how the new view is
|
2414
|
-
// appended to the `$el` that the region is managing
|
2415
|
-
open: function(view){
|
2416
|
-
this.$el.html(view.el);
|
2417
|
-
},
|
2418
|
-
|
2419
|
-
// Close the current view, if there is one. If there is no
|
2420
|
-
// current view, it does nothing and returns immediately.
|
2421
|
-
close: function(){
|
2422
|
-
var view = this.currentView;
|
2423
|
-
if (!view || view.isClosed){ return; }
|
2424
|
-
|
2425
|
-
if (view.close) { view.close(); }
|
2426
|
-
this.trigger("view:closed", view);
|
2427
|
-
|
2428
|
-
delete this.currentView;
|
2429
|
-
},
|
2430
|
-
|
2431
|
-
// Attach an existing view to the region. This
|
2432
|
-
// will not call `render` or `onShow` for the new view,
|
2433
|
-
// and will not replace the current HTML for the `el`
|
2434
|
-
// of the region.
|
2435
|
-
attachView: function(view){
|
2436
|
-
this.currentView = view;
|
2437
|
-
},
|
2438
|
-
|
2439
|
-
// Reset the region by closing any existing view and
|
2440
|
-
// clearing out the cached `$el`. The next time a view
|
2441
|
-
// is shown via this region, the region will re-query the
|
2442
|
-
// DOM for the region's `el`.
|
2443
|
-
reset: function(){
|
2444
|
-
this.close();
|
2445
|
-
delete this.$el;
|
2446
|
-
}
|
2447
|
-
});
|
2448
|
-
|
2449
|
-
// Copy the `extend` function used by Backbone's classes
|
2450
|
-
Marionette.Region.extend = Marionette.extend;
|
2451
|
-
|
2452
|
-
// Layout
|
2453
|
-
// ------
|
2454
|
-
|
2455
|
-
// Used for managing application layouts, nested layouts and
|
2456
|
-
// multiple regions within an application or sub-application.
|
2457
|
-
//
|
2458
|
-
// A specialized view type that renders an area of HTML and then
|
2459
|
-
// attaches `Region` instances to the specified `regions`.
|
2460
|
-
// Used for composite view management and sub-application areas.
|
2461
|
-
Marionette.Layout = Marionette.ItemView.extend({
|
2462
|
-
regionType: Marionette.Region,
|
2463
|
-
|
2464
|
-
// Ensure the regions are avialable when the `initialize` method
|
2465
|
-
// is called.
|
2466
|
-
constructor: function () {
|
2467
|
-
this.initializeRegions();
|
2468
|
-
Backbone.Marionette.ItemView.apply(this, arguments);
|
2469
|
-
},
|
2470
|
-
|
2471
|
-
// Layout's render will use the existing region objects the
|
2472
|
-
// first time it is called. Subsequent calls will close the
|
2473
|
-
// views that the regions are showing and then reset the `el`
|
2474
|
-
// for the regions to the newly rendered DOM elements.
|
2475
|
-
render: function(){
|
2476
|
-
// If this is not the first render call, then we need to
|
2477
|
-
// re-initializing the `el` for each region
|
2478
|
-
if (!this._firstRender){
|
2479
|
-
this.closeRegions();
|
2480
|
-
this.reInitializeRegions();
|
2481
|
-
} else {
|
2482
|
-
this._firstRender = false;
|
2483
|
-
}
|
2484
|
-
|
2485
|
-
var result = Marionette.ItemView.prototype.render.apply(this, arguments);
|
2486
|
-
return result;
|
2487
|
-
},
|
2488
|
-
|
2489
|
-
// Handle closing regions, and then close the view itself.
|
2490
|
-
close: function () {
|
2491
|
-
if (this.isClosed){ return; }
|
2492
|
-
|
2493
|
-
this.closeRegions();
|
2494
|
-
this.destroyRegions();
|
2495
|
-
Backbone.Marionette.ItemView.prototype.close.call(this, arguments);
|
2496
|
-
},
|
2497
|
-
|
2498
|
-
// Initialize the regions that have been defined in a
|
2499
|
-
// `regions` attribute on this layout. The key of the
|
2500
|
-
// hash becomes an attribute on the layout object directly.
|
2501
|
-
// For example: `regions: { menu: ".menu-container" }`
|
2502
|
-
// will product a `layout.menu` object which is a region
|
2503
|
-
// that controls the `.menu-container` DOM element.
|
2504
|
-
initializeRegions: function () {
|
2505
|
-
if (!this.regionManagers){
|
2506
|
-
this.regionManagers = {};
|
2507
|
-
}
|
2508
|
-
|
2509
|
-
var that = this;
|
2510
|
-
var regions = this.regions || {};
|
2511
|
-
_.each(regions, function (region, name) {
|
2512
|
-
|
2513
|
-
var regionManager = Marionette.Region.buildRegion(region, that.regionType);
|
2514
|
-
regionManager.getEl = function(selector){
|
2515
|
-
return that.$(selector);
|
2516
|
-
};
|
2517
|
-
|
2518
|
-
that.regionManagers[name] = regionManager;
|
2519
|
-
that[name] = regionManager;
|
2520
|
-
});
|
2521
|
-
|
2522
|
-
},
|
2523
|
-
|
2524
|
-
// Re-initialize all of the regions by updating the `el` that
|
2525
|
-
// they point to
|
2526
|
-
reInitializeRegions: function(){
|
2527
|
-
if (this.regionManagers && _.size(this.regionManagers)===0){
|
2528
|
-
this.initializeRegions();
|
2529
|
-
} else {
|
2530
|
-
_.each(this.regionManagers, function(region){
|
2531
|
-
region.reset();
|
2532
|
-
});
|
2533
|
-
}
|
2534
|
-
},
|
2535
|
-
|
2536
|
-
// Close all of the regions that have been opened by
|
2537
|
-
// this layout. This method is called when the layout
|
2538
|
-
// itself is closed.
|
2539
|
-
closeRegions: function () {
|
2540
|
-
var that = this;
|
2541
|
-
_.each(this.regionManagers, function (manager, name) {
|
2542
|
-
manager.close();
|
2543
|
-
});
|
2544
|
-
},
|
2545
|
-
|
2546
|
-
// Destroys all of the regions by removing references
|
2547
|
-
// from the Layout
|
2548
|
-
destroyRegions: function(){
|
2549
|
-
var that = this;
|
2550
|
-
_.each(this.regionManagers, function (manager, name) {
|
2551
|
-
delete that[name];
|
2552
|
-
});
|
2553
|
-
this.regionManagers = {};
|
2554
|
-
}
|
2555
|
-
});
|
2556
|
-
|
2557
|
-
|
2558
|
-
// Application
|
2559
|
-
// -----------
|
2560
|
-
|
2561
|
-
// Contain and manage the composite application as a whole.
|
2562
|
-
// Stores and starts up `Region` objects, includes an
|
2563
|
-
// event aggregator as `app.vent`
|
2564
|
-
Marionette.Application = function(options){
|
2565
|
-
this.initCallbacks = new Marionette.Callbacks();
|
2566
|
-
this.vent = new Marionette.EventAggregator();
|
2567
|
-
this.commands = new Backbone.Wreqr.Commands();
|
2568
|
-
this.reqres = new Backbone.Wreqr.RequestResponse();
|
2569
|
-
this.submodules = {};
|
2570
|
-
|
2571
|
-
_.extend(this, options);
|
2572
|
-
|
2573
|
-
Marionette.addEventBinder(this);
|
2574
|
-
};
|
2575
|
-
|
2576
|
-
_.extend(Marionette.Application.prototype, Backbone.Events, {
|
2577
|
-
// Command execution, facilitated by Backbone.Wreqr.Commands
|
2578
|
-
execute: function(){
|
2579
|
-
this.commands.execute.apply(this.commands, arguments);
|
2580
|
-
},
|
2581
|
-
|
2582
|
-
// Request/response, facilitated by Backbone.Wreqr.RequestResponse
|
2583
|
-
request: function(){
|
2584
|
-
return this.reqres.request.apply(this.reqres, arguments);
|
2585
|
-
},
|
2586
|
-
|
2587
|
-
// Add an initializer that is either run at when the `start`
|
2588
|
-
// method is called, or run immediately if added after `start`
|
2589
|
-
// has already been called.
|
2590
|
-
addInitializer: function(initializer){
|
2591
|
-
this.initCallbacks.add(initializer);
|
2592
|
-
},
|
2593
|
-
|
2594
|
-
// kick off all of the application's processes.
|
2595
|
-
// initializes all of the regions that have been added
|
2596
|
-
// to the app, and runs all of the initializer functions
|
2597
|
-
start: function(options){
|
2598
|
-
this.trigger("initialize:before", options);
|
2599
|
-
this.initCallbacks.run(options, this);
|
2600
|
-
this.trigger("initialize:after", options);
|
2601
|
-
|
2602
|
-
this.trigger("start", options);
|
2603
|
-
},
|
2604
|
-
|
2605
|
-
// Add regions to your app.
|
2606
|
-
// Accepts a hash of named strings or Region objects
|
2607
|
-
// addRegions({something: "#someRegion"})
|
2608
|
-
// addRegions{{something: Region.extend({el: "#someRegion"}) });
|
2609
|
-
addRegions: function(regions){
|
2610
|
-
var that = this;
|
2611
|
-
_.each(regions, function (region, name) {
|
2612
|
-
var regionManager = Marionette.Region.buildRegion(region, Marionette.Region);
|
2613
|
-
that[name] = regionManager;
|
2614
|
-
});
|
2615
|
-
},
|
2616
|
-
|
2617
|
-
// Removes a region from your app.
|
2618
|
-
// Accepts the regions name
|
2619
|
-
// removeRegion('myRegion')
|
2620
|
-
removeRegion: function(region) {
|
2621
|
-
this[region].close();
|
2622
|
-
delete this[region];
|
2623
|
-
},
|
2624
|
-
|
2625
|
-
// Create a module, attached to the application
|
2626
|
-
module: function(moduleNames, moduleDefinition){
|
2627
|
-
// slice the args, and add this application object as the
|
2628
|
-
// first argument of the array
|
2629
|
-
var args = slice.call(arguments);
|
2630
|
-
args.unshift(this);
|
2631
|
-
|
2632
|
-
// see the Marionette.Module object for more information
|
2633
|
-
return Marionette.Module.create.apply(Marionette.Module, args);
|
2634
|
-
}
|
2635
|
-
});
|
2636
|
-
|
2637
|
-
// Copy the `extend` function used by Backbone's classes
|
2638
|
-
Marionette.Application.extend = Marionette.extend;
|
2639
|
-
|
2640
|
-
// AppRouter
|
2641
|
-
// ---------
|
2642
|
-
|
2643
|
-
// Reduce the boilerplate code of handling route events
|
2644
|
-
// and then calling a single method on another object.
|
2645
|
-
// Have your routers configured to call the method on
|
2646
|
-
// your object, directly.
|
2647
|
-
//
|
2648
|
-
// Configure an AppRouter with `appRoutes`.
|
2649
|
-
//
|
2650
|
-
// App routers can only take one `controller` object.
|
2651
|
-
// It is recommended that you divide your controller
|
2652
|
-
// objects in to smaller peices of related functionality
|
2653
|
-
// and have multiple routers / controllers, instead of
|
2654
|
-
// just one giant router and controller.
|
2655
|
-
//
|
2656
|
-
// You can also add standard routes to an AppRouter.
|
2657
|
-
|
2658
|
-
Marionette.AppRouter = Backbone.Router.extend({
|
2659
|
-
|
2660
|
-
constructor: function(options){
|
2661
|
-
Backbone.Router.prototype.constructor.call(this, options);
|
2662
|
-
|
2663
|
-
if (this.appRoutes){
|
2664
|
-
var controller = this.controller;
|
2665
|
-
if (options && options.controller) {
|
2666
|
-
controller = options.controller;
|
2667
|
-
}
|
2668
|
-
this.processAppRoutes(controller, this.appRoutes);
|
2669
|
-
}
|
2670
|
-
},
|
2671
|
-
|
2672
|
-
// Internal method to process the `appRoutes` for the
|
2673
|
-
// router, and turn them in to routes that trigger the
|
2674
|
-
// specified method on the specified `controller`.
|
2675
|
-
processAppRoutes: function(controller, appRoutes){
|
2676
|
-
var method, methodName;
|
2677
|
-
var route, routesLength, i;
|
2678
|
-
var routes = [];
|
2679
|
-
var router = this;
|
2680
|
-
|
2681
|
-
for(route in appRoutes){
|
2682
|
-
if (appRoutes.hasOwnProperty(route)){
|
2683
|
-
routes.unshift([route, appRoutes[route]]);
|
2684
|
-
}
|
2685
|
-
}
|
2686
|
-
|
2687
|
-
routesLength = routes.length;
|
2688
|
-
for (i = 0; i < routesLength; i++){
|
2689
|
-
route = routes[i][0];
|
2690
|
-
methodName = routes[i][1];
|
2691
|
-
method = controller[methodName];
|
2692
|
-
|
2693
|
-
if (!method){
|
2694
|
-
var msg = "Method '" + methodName + "' was not found on the controller";
|
2695
|
-
var err = new Error(msg);
|
2696
|
-
err.name = "NoMethodError";
|
2697
|
-
throw err;
|
2698
|
-
}
|
2699
|
-
|
2700
|
-
method = _.bind(method, controller);
|
2701
|
-
router.route(route, methodName, method);
|
2702
|
-
}
|
2703
|
-
}
|
2704
|
-
});
|
2705
|
-
|
2706
|
-
|
2707
|
-
// Module
|
2708
|
-
// ------
|
2709
|
-
|
2710
|
-
// A simple module system, used to create privacy and encapsulation in
|
2711
|
-
// Marionette applications
|
2712
|
-
Marionette.Module = function(moduleName, app){
|
2713
|
-
this.moduleName = moduleName;
|
2714
|
-
|
2715
|
-
// store sub-modules
|
2716
|
-
this.submodules = {};
|
2717
|
-
|
2718
|
-
this._setupInitializersAndFinalizers();
|
2719
|
-
|
2720
|
-
// store the configuration for this module
|
2721
|
-
this.config = {};
|
2722
|
-
this.config.app = app;
|
2723
|
-
|
2724
|
-
// extend this module with an event binder
|
2725
|
-
Marionette.addEventBinder(this);
|
2726
|
-
};
|
2727
|
-
|
2728
|
-
// Extend the Module prototype with events / bindTo, so that the module
|
2729
|
-
// can be used as an event aggregator or pub/sub.
|
2730
|
-
_.extend(Marionette.Module.prototype, Backbone.Events, {
|
2731
|
-
|
2732
|
-
// Initializer for a specific module. Initializers are run when the
|
2733
|
-
// module's `start` method is called.
|
2734
|
-
addInitializer: function(callback){
|
2735
|
-
this._initializerCallbacks.add(callback);
|
2736
|
-
},
|
2737
|
-
|
2738
|
-
// Finalizers are run when a module is stopped. They are used to teardown
|
2739
|
-
// and finalize any variables, references, events and other code that the
|
2740
|
-
// module had set up.
|
2741
|
-
addFinalizer: function(callback){
|
2742
|
-
this._finalizerCallbacks.add(callback);
|
2743
|
-
},
|
2744
|
-
|
2745
|
-
// Start the module, and run all of it's initializers
|
2746
|
-
start: function(options){
|
2747
|
-
// Prevent re-start the module
|
2748
|
-
if (this._isInitialized){ return; }
|
2749
|
-
|
2750
|
-
// start the sub-modules (depth-first hierarchy)
|
2751
|
-
_.each(this.submodules, function(mod){
|
2752
|
-
if (mod.config.options.startWithParent){
|
2753
|
-
mod.start(options);
|
2754
|
-
}
|
2755
|
-
});
|
2756
|
-
|
2757
|
-
// run the callbacks to "start" the current module
|
2758
|
-
this._initializerCallbacks.run(options, this);
|
2759
|
-
this._isInitialized = true;
|
2760
|
-
},
|
2761
|
-
|
2762
|
-
// Stop this module by running its finalizers and then stop all of
|
2763
|
-
// the sub-modules for this module
|
2764
|
-
stop: function(){
|
2765
|
-
// if we are not initialized, don't bother finalizing
|
2766
|
-
if (!this._isInitialized){ return; }
|
2767
|
-
this._isInitialized = false;
|
2768
|
-
|
2769
|
-
// stop the sub-modules; depth-first, to make sure the
|
2770
|
-
// sub-modules are stopped / finalized before parents
|
2771
|
-
_.each(this.submodules, function(mod){ mod.stop(); });
|
2772
|
-
|
2773
|
-
// run the finalizers
|
2774
|
-
this._finalizerCallbacks.run();
|
2775
|
-
|
2776
|
-
// reset the initializers and finalizers
|
2777
|
-
this._initializerCallbacks.reset();
|
2778
|
-
this._finalizerCallbacks.reset();
|
2779
|
-
},
|
2780
|
-
|
2781
|
-
// Configure the module with a definition function and any custom args
|
2782
|
-
// that are to be passed in to the definition function
|
2783
|
-
addDefinition: function(moduleDefinition, customArgs){
|
2784
|
-
this._runModuleDefinition(moduleDefinition, customArgs);
|
2785
|
-
},
|
2786
|
-
|
2787
|
-
// Internal method: run the module definition function with the correct
|
2788
|
-
// arguments
|
2789
|
-
_runModuleDefinition: function(definition, customArgs){
|
2790
|
-
if (!definition){ return; }
|
2791
|
-
|
2792
|
-
// build the correct list of arguments for the module definition
|
2793
|
-
var args = _.flatten([
|
2794
|
-
this,
|
2795
|
-
this.config.app,
|
2796
|
-
Backbone,
|
2797
|
-
Marionette,
|
2798
|
-
$, _,
|
2799
|
-
customArgs
|
2800
|
-
]);
|
2801
|
-
|
2802
|
-
definition.apply(this, args);
|
2803
|
-
},
|
2804
|
-
|
2805
|
-
// Internal method: set up new copies of initializers and finalizers.
|
2806
|
-
// Calling this method will wipe out all existing initializers and
|
2807
|
-
// finalizers.
|
2808
|
-
_setupInitializersAndFinalizers: function(){
|
2809
|
-
this._initializerCallbacks = new Marionette.Callbacks();
|
2810
|
-
this._finalizerCallbacks = new Marionette.Callbacks();
|
2811
|
-
}
|
2812
|
-
});
|
2813
|
-
|
2814
|
-
// Function level methods to create modules
|
2815
|
-
_.extend(Marionette.Module, {
|
2816
|
-
|
2817
|
-
// Create a module, hanging off the app parameter as the parent object.
|
2818
|
-
create: function(app, moduleNames, moduleDefinition){
|
2819
|
-
var that = this;
|
2820
|
-
var parentModule = app;
|
2821
|
-
moduleNames = moduleNames.split(".");
|
2822
|
-
|
2823
|
-
// get the custom args passed in after the module definition and
|
2824
|
-
// get rid of the module name and definition function
|
2825
|
-
var customArgs = slice.apply(arguments);
|
2826
|
-
customArgs.splice(0, 3);
|
2827
|
-
|
2828
|
-
// Loop through all the parts of the module definition
|
2829
|
-
var length = moduleNames.length;
|
2830
|
-
_.each(moduleNames, function(moduleName, i){
|
2831
|
-
var isLastModuleInChain = (i === length-1);
|
2832
|
-
|
2833
|
-
var module = that._getModuleDefinition(parentModule, moduleName, app);
|
2834
|
-
module.config.options = that._getModuleOptions(parentModule, moduleDefinition);
|
2835
|
-
|
2836
|
-
// if it's the first module in the chain, configure it
|
2837
|
-
// for auto-start, as specified by the options
|
2838
|
-
if (isLastModuleInChain){
|
2839
|
-
that._configureAutoStart(app, module);
|
2840
|
-
}
|
2841
|
-
|
2842
|
-
// Only add a module definition and initializer when this is
|
2843
|
-
// the last module in a "parent.child.grandchild" hierarchy of
|
2844
|
-
// module names
|
2845
|
-
if (isLastModuleInChain && module.config.options.hasDefinition){
|
2846
|
-
module.addDefinition(module.config.options.definition, customArgs);
|
2847
|
-
}
|
2848
|
-
|
2849
|
-
// Reset the parent module so that the next child
|
2850
|
-
// in the list will be added to the correct parent
|
2851
|
-
parentModule = module;
|
2852
|
-
});
|
2853
|
-
|
2854
|
-
// Return the last module in the definition chain
|
2855
|
-
return parentModule;
|
2856
|
-
},
|
2857
|
-
|
2858
|
-
_configureAutoStart: function(app, module){
|
2859
|
-
// Only add the initializer if it's the first module, and
|
2860
|
-
// if it is set to auto-start, and if it has not yet been added
|
2861
|
-
if (module.config.options.startWithParent && !module.config.autoStartConfigured){
|
2862
|
-
// start the module when the app starts
|
2863
|
-
app.addInitializer(function(options){
|
2864
|
-
module.start(options);
|
2865
|
-
});
|
2866
|
-
}
|
2867
|
-
|
2868
|
-
// prevent this module from being configured for
|
2869
|
-
// auto start again. the first time the module
|
2870
|
-
// is defined, determines it's auto-start
|
2871
|
-
module.config.autoStartConfigured = true;
|
2872
|
-
},
|
2873
|
-
|
2874
|
-
_getModuleDefinition: function(parentModule, moduleName, app){
|
2875
|
-
// Get an existing module of this name if we have one
|
2876
|
-
var module = parentModule[moduleName];
|
2877
|
-
|
2878
|
-
if (!module){
|
2879
|
-
// Create a new module if we don't have one
|
2880
|
-
module = new Marionette.Module(moduleName, app);
|
2881
|
-
parentModule[moduleName] = module;
|
2882
|
-
// store the module on the parent
|
2883
|
-
parentModule.submodules[moduleName] = module;
|
2884
|
-
}
|
2885
|
-
|
2886
|
-
return module;
|
2887
|
-
},
|
2888
|
-
|
2889
|
-
_getModuleOptions: function(parentModule, moduleDefinition){
|
2890
|
-
// default to starting the module with the app
|
2891
|
-
var options = {
|
2892
|
-
startWithParent: true,
|
2893
|
-
hasDefinition: !!moduleDefinition
|
2894
|
-
};
|
2895
|
-
|
2896
|
-
// short circuit if we don't have a module definition
|
2897
|
-
if (!options.hasDefinition){ return options; }
|
2898
|
-
|
2899
|
-
if (_.isFunction(moduleDefinition)){
|
2900
|
-
// if the definition is a function, assign it directly
|
2901
|
-
// and use the defaults
|
2902
|
-
options.definition = moduleDefinition;
|
2903
|
-
|
2904
|
-
} else {
|
2905
|
-
|
2906
|
-
// the definition is an object.
|
2907
|
-
|
2908
|
-
// grab the "define" attribute
|
2909
|
-
options.hasDefinition = !!moduleDefinition.define;
|
2910
|
-
options.definition = moduleDefinition.define;
|
2911
|
-
|
2912
|
-
// grab the "startWithParent" attribute if one exists
|
2913
|
-
if (moduleDefinition.hasOwnProperty("startWithParent")){
|
2914
|
-
options.startWithParent = moduleDefinition.startWithParent;
|
2915
|
-
}
|
2916
|
-
}
|
2917
|
-
|
2918
|
-
return options;
|
2919
|
-
}
|
2920
|
-
});
|
2921
|
-
|
2922
|
-
// Template Cache
|
2923
|
-
// --------------
|
2924
|
-
|
2925
|
-
// Manage templates stored in `<script>` blocks,
|
2926
|
-
// caching them for faster access.
|
2927
|
-
Marionette.TemplateCache = function(templateId){
|
2928
|
-
this.templateId = templateId;
|
2929
|
-
};
|
2930
|
-
|
2931
|
-
// TemplateCache object-level methods. Manage the template
|
2932
|
-
// caches from these method calls instead of creating
|
2933
|
-
// your own TemplateCache instances
|
2934
|
-
_.extend(Marionette.TemplateCache, {
|
2935
|
-
templateCaches: {},
|
2936
|
-
|
2937
|
-
// Get the specified template by id. Either
|
2938
|
-
// retrieves the cached version, or loads it
|
2939
|
-
// from the DOM.
|
2940
|
-
get: function(templateId){
|
2941
|
-
var that = this;
|
2942
|
-
var cachedTemplate = this.templateCaches[templateId];
|
2943
|
-
|
2944
|
-
if (!cachedTemplate){
|
2945
|
-
cachedTemplate = new Marionette.TemplateCache(templateId);
|
2946
|
-
this.templateCaches[templateId] = cachedTemplate;
|
2947
|
-
}
|
2948
|
-
|
2949
|
-
return cachedTemplate.load();
|
2950
|
-
},
|
2951
|
-
|
2952
|
-
// Clear templates from the cache. If no arguments
|
2953
|
-
// are specified, clears all templates:
|
2954
|
-
// `clear()`
|
2955
|
-
//
|
2956
|
-
// If arguments are specified, clears each of the
|
2957
|
-
// specified templates from the cache:
|
2958
|
-
// `clear("#t1", "#t2", "...")`
|
2959
|
-
clear: function(){
|
2960
|
-
var i;
|
2961
|
-
var length = arguments.length;
|
2962
|
-
|
2963
|
-
if (length > 0){
|
2964
|
-
for(i=0; i<length; i++){
|
2965
|
-
delete this.templateCaches[arguments[i]];
|
2966
|
-
}
|
2967
|
-
} else {
|
2968
|
-
this.templateCaches = {};
|
2969
|
-
}
|
2970
|
-
}
|
2971
|
-
});
|
2972
|
-
|
2973
|
-
// TemplateCache instance methods, allowing each
|
2974
|
-
// template cache object to manage it's own state
|
2975
|
-
// and know whether or not it has been loaded
|
2976
|
-
_.extend(Marionette.TemplateCache.prototype, {
|
2977
|
-
|
2978
|
-
// Internal method to load the template asynchronously.
|
2979
|
-
load: function(){
|
2980
|
-
var that = this;
|
2981
|
-
|
2982
|
-
// Guard clause to prevent loading this template more than once
|
2983
|
-
if (this.compiledTemplate){
|
2984
|
-
return this.compiledTemplate;
|
2985
|
-
}
|
2986
|
-
|
2987
|
-
// Load the template and compile it
|
2988
|
-
var template = this.loadTemplate(this.templateId);
|
2989
|
-
this.compiledTemplate = this.compileTemplate(template);
|
2990
|
-
|
2991
|
-
return this.compiledTemplate;
|
2992
|
-
},
|
2993
|
-
|
2994
|
-
// Load a template from the DOM, by default. Override
|
2995
|
-
// this method to provide your own template retrieval,
|
2996
|
-
// such as asynchronous loading from a server.
|
2997
|
-
loadTemplate: function(templateId){
|
2998
|
-
var template = $(templateId).html();
|
2999
|
-
|
3000
|
-
if (!template || template.length === 0){
|
3001
|
-
var msg = "Could not find template: '" + templateId + "'";
|
3002
|
-
var err = new Error(msg);
|
3003
|
-
err.name = "NoTemplateError";
|
3004
|
-
throw err;
|
3005
|
-
}
|
3006
|
-
|
3007
|
-
return template;
|
3008
|
-
},
|
3009
|
-
|
3010
|
-
// Pre-compile the template before caching it. Override
|
3011
|
-
// this method if you do not need to pre-compile a template
|
3012
|
-
// (JST / RequireJS for example) or if you want to change
|
3013
|
-
// the template engine used (Handebars, etc).
|
3014
|
-
compileTemplate: function(rawTemplate){
|
3015
|
-
return _.template(rawTemplate);
|
3016
|
-
}
|
3017
|
-
});
|
3018
|
-
|
3019
|
-
|
3020
|
-
// Renderer
|
3021
|
-
// --------
|
3022
|
-
|
3023
|
-
// Render a template with data by passing in the template
|
3024
|
-
// selector and the data to render.
|
3025
|
-
Marionette.Renderer = {
|
3026
|
-
|
3027
|
-
// Render a template with data. The `template` parameter is
|
3028
|
-
// passed to the `TemplateCache` object to retrieve the
|
3029
|
-
// template function. Override this method to provide your own
|
3030
|
-
// custom rendering and template handling for all of Marionette.
|
3031
|
-
render: function(template, data){
|
3032
|
-
var templateFunc = typeof template === 'function' ? template : Marionette.TemplateCache.get(template);
|
3033
|
-
var html = templateFunc(data);
|
3034
|
-
return html;
|
3035
|
-
}
|
3036
|
-
};
|
3037
|
-
|
3038
|
-
|
3039
|
-
// Callbacks
|
3040
|
-
// ---------
|
3041
|
-
|
3042
|
-
// A simple way of managing a collection of callbacks
|
3043
|
-
// and executing them at a later point in time, using jQuery's
|
3044
|
-
// `Deferred` object.
|
3045
|
-
Marionette.Callbacks = function(){
|
3046
|
-
this._deferred = $.Deferred();
|
3047
|
-
this._callbacks = [];
|
3048
|
-
};
|
3049
|
-
|
3050
|
-
_.extend(Marionette.Callbacks.prototype, {
|
3051
|
-
|
3052
|
-
// Add a callback to be executed. Callbacks added here are
|
3053
|
-
// guaranteed to execute, even if they are added after the
|
3054
|
-
// `run` method is called.
|
3055
|
-
add: function(callback, contextOverride){
|
3056
|
-
this._callbacks.push({cb: callback, ctx: contextOverride});
|
3057
|
-
|
3058
|
-
this._deferred.done(function(context, options){
|
3059
|
-
if (contextOverride){ context = contextOverride; }
|
3060
|
-
callback.call(context, options);
|
3061
|
-
});
|
3062
|
-
},
|
3063
|
-
|
3064
|
-
// Run all registered callbacks with the context specified.
|
3065
|
-
// Additional callbacks can be added after this has been run
|
3066
|
-
// and they will still be executed.
|
3067
|
-
run: function(options, context){
|
3068
|
-
this._deferred.resolve(context, options);
|
3069
|
-
},
|
3070
|
-
|
3071
|
-
// Resets the list of callbacks to be run, allowing the same list
|
3072
|
-
// to be run multiple times - whenever the `run` method is called.
|
3073
|
-
reset: function(){
|
3074
|
-
var that = this;
|
3075
|
-
var callbacks = this._callbacks;
|
3076
|
-
this._deferred = $.Deferred();
|
3077
|
-
this._callbacks = [];
|
3078
|
-
_.each(callbacks, function(cb){
|
3079
|
-
that.add(cb.cb, cb.ctx);
|
3080
|
-
});
|
3081
|
-
}
|
3082
|
-
});
|
3083
|
-
|
3084
|
-
|
3085
|
-
// Event Aggregator
|
3086
|
-
// ----------------
|
3087
|
-
// A pub-sub object that can be used to decouple various parts
|
3088
|
-
// of an application through event-driven architecture.
|
3089
|
-
//
|
3090
|
-
// https://github.com/marionettejs/backbone.wreqr
|
3091
|
-
Marionette.EventAggregator = Backbone.Wreqr.EventAggregator;
|
3092
|
-
|
3093
|
-
|
3094
|
-
return Marionette;
|
3095
|
-
})(Backbone, _, $ || window.jQuery || window.Zepto || window.ender);
|
3096
|
-
/*!
|
3097
|
-
* Tableling v0.0.9
|
3098
|
-
* Copyright (c) 2012 Simon Oulevay (Alpha Hydrae) <hydrae.alpha@gmail.com>
|
3099
|
-
* Distributed under MIT license
|
3100
|
-
* https://github.com/AlphaHydrae/tableling
|
3101
|
-
*/
|
3102
|
-
Backbone.Tableling = Tableling = (function(Backbone, _, $){
|
3103
|
-
|
3104
|
-
var Tableling = {
|
3105
|
-
version : "0.0.9"
|
3106
|
-
};
|
3107
|
-
|
3108
|
-
// Tableling
|
3109
|
-
// ---------
|
3110
|
-
//
|
3111
|
-
// A tableling table is a Marionette layout which fetches data
|
3112
|
-
// from a Backbone collection. It is controlled with an EventAggregator.
|
3113
|
-
Tableling.Table = Backbone.Marionette.Layout.extend({
|
3114
|
-
|
3115
|
-
className: 'tableling',
|
3116
|
-
|
3117
|
-
// Default table options can be overriden by subclasses.
|
3118
|
-
tableling : {
|
3119
|
-
page : 1,
|
3120
|
-
pageSize : 15
|
3121
|
-
},
|
3122
|
-
|
3123
|
-
initialize : function(options) {
|
3124
|
-
options = options || {};
|
3125
|
-
|
3126
|
-
// Table options can also be overriden for each instance at construction.
|
3127
|
-
this.tableling = _.extend(_.clone(this.tableling), this.filterConfig(options));
|
3128
|
-
|
3129
|
-
// We use an event aggregator to manage the layout and its components.
|
3130
|
-
// You can use your own by passing a `vent` option.
|
3131
|
-
this.vent = options.vent || new Backbone.Marionette.EventAggregator();
|
3132
|
-
|
3133
|
-
this.fetchOptions = _.extend(_.clone(this.fetchOptions || {}), options.fetchOptions || {});
|
3134
|
-
|
3135
|
-
// Components should trigger the `table:update` event to update
|
3136
|
-
// the table (e.g. change page size, sort) and fetch the new data.
|
3137
|
-
this.vent.on('table:update', this.update, this);
|
3138
|
-
|
3139
|
-
this.on('render', this.setup, this);
|
3140
|
-
},
|
3141
|
-
|
3142
|
-
// Called once rendering is complete. By default, it updates the table.
|
3143
|
-
setup : function() {
|
3144
|
-
this.vent.trigger('table:setup', this.filterConfig(this.tableling, true));
|
3145
|
-
this.vent.trigger('table:update');
|
3146
|
-
},
|
3147
|
-
|
3148
|
-
// Subclasses must return the Backbone.Collection used to fetch data.
|
3149
|
-
getCollection : function() {
|
3150
|
-
throw new Error('#getCollection not implemented. It should return the Backbone.Collection instance used to fetch data.');
|
3151
|
-
},
|
3152
|
-
|
3153
|
-
// ### Refreshing the table
|
3154
|
-
update : function(config, options) {
|
3155
|
-
|
3156
|
-
_.each(this.filterConfig(config || {}), _.bind(this.updateValue, this));
|
3157
|
-
|
3158
|
-
// Set the `refresh` option to false to update the table configuration
|
3159
|
-
// without refreshing.
|
3160
|
-
if (!options || typeof(options.refresh) == 'undefined' || options.refresh) {
|
3161
|
-
this.refresh();
|
3162
|
-
}
|
3163
|
-
},
|
3164
|
-
|
3165
|
-
updateValue : function(value, key) {
|
3166
|
-
if (value && value.toString().length) {
|
3167
|
-
this.tableling[key] = value;
|
3168
|
-
} else {
|
3169
|
-
// Blank values are deleted to avoid sending them in ajax requests.
|
3170
|
-
delete this.tableling[key];
|
3171
|
-
}
|
3172
|
-
},
|
3173
|
-
|
3174
|
-
refresh : function() {
|
3175
|
-
|
3176
|
-
// You can provide `fetchOptions` to add properties to the
|
3177
|
-
// fetch request.
|
3178
|
-
//
|
3179
|
-
// var MyTable = Tableling.Table.extend({
|
3180
|
-
// fetchOptions : {
|
3181
|
-
// type : 'POST' // fetch data with POST
|
3182
|
-
// }
|
3183
|
-
// });
|
3184
|
-
//
|
3185
|
-
// // You can also override for each instance.
|
3186
|
-
// new MyTable({
|
3187
|
-
// fetchOptions : {
|
3188
|
-
// type : 'GET'
|
3189
|
-
// }
|
3190
|
-
// });
|
3191
|
-
var options = _.clone(this.fetchOptions);
|
3192
|
-
options.data = this.requestData();
|
3193
|
-
options.success = _.bind(this.processResponse, this);
|
3194
|
-
|
3195
|
-
// `table:refreshing` is triggered every time new data is being fetched.
|
3196
|
-
// The first argument is the request data.
|
3197
|
-
this.ventTrigger('table:refreshing', options.data);
|
3198
|
-
|
3199
|
-
this.getCollection().fetch(options);
|
3200
|
-
},
|
3201
|
-
|
3202
|
-
// ### Request
|
3203
|
-
requestData : function() {
|
3204
|
-
return this.filterConfig(this.tableling);
|
3205
|
-
},
|
3206
|
-
|
3207
|
-
// ### Response
|
3208
|
-
processResponse : function(collection, response) {
|
3209
|
-
|
3210
|
-
this.tableling.length = collection.length;
|
3211
|
-
|
3212
|
-
// Tableling expects the response from a fetch to have a `total` property
|
3213
|
-
// which is the total number of items (not just in the current page).
|
3214
|
-
this.tableling.total = response.total;
|
3215
|
-
|
3216
|
-
// `tableling:refreshed` is triggered after every refresh. The first argument
|
3217
|
-
// is the current table configuration with the following additional meta data:
|
3218
|
-
//
|
3219
|
-
// * `total` - the total number of items
|
3220
|
-
// * `length` - the number of items in the current page
|
3221
|
-
this.ventTrigger('table:refreshed', this.filterConfig(this.tableling, true));
|
3222
|
-
},
|
3223
|
-
|
3224
|
-
// ### Utilities
|
3225
|
-
// Whitelists the given configuration to contain only table configuration properties.
|
3226
|
-
// Pass `true` as the second argument to include meta data (i.e. total & length).
|
3227
|
-
filterConfig : function(config, all) {
|
3228
|
-
if (all) {
|
3229
|
-
return _.pick(config, 'page', 'pageSize', 'quickSearch', 'sort', 'length', 'total');
|
3230
|
-
} else {
|
3231
|
-
return _.pick(config, 'page', 'pageSize', 'quickSearch', 'sort');
|
3232
|
-
}
|
3233
|
-
},
|
3234
|
-
|
3235
|
-
// Triggers an event in the event aggregator. If `Tableling.debug` is set, it also
|
3236
|
-
// logs the event and its arguments.
|
3237
|
-
ventTrigger : function() {
|
3238
|
-
|
3239
|
-
var args = Array.prototype.slice.call(arguments);
|
3240
|
-
this.vent.trigger.apply(this.vent, args);
|
3241
|
-
|
3242
|
-
if (Tableling.debug) {
|
3243
|
-
console.log(args.shift() + ' - ' + JSON.stringify(args));
|
3244
|
-
}
|
3245
|
-
}
|
3246
|
-
});
|
3247
|
-
|
3248
|
-
// Tableling.Collection
|
3249
|
-
// --------------------
|
3250
|
-
//
|
3251
|
-
// Tableling expects fetch responses to have a `total` property in addition
|
3252
|
-
// to the model data. You can extend this Backbone.Collection subclass which
|
3253
|
-
// expects the following response format:
|
3254
|
-
//
|
3255
|
-
// {
|
3256
|
-
// "total": 12,
|
3257
|
-
// "data": [
|
3258
|
-
// { /* ... model data ... */ },
|
3259
|
-
// { /* ... model data ... */ }
|
3260
|
-
// ]
|
3261
|
-
// }
|
3262
|
-
Tableling.Collection = Backbone.Collection.extend({
|
3263
|
-
|
3264
|
-
parse : function(response) {
|
3265
|
-
return response.data;
|
3266
|
-
}
|
3267
|
-
});
|
3268
|
-
|
3269
|
-
// Implementations
|
3270
|
-
// ---------------
|
3271
|
-
//
|
3272
|
-
// <a href="tableling.bootstrap.html">tableling.bootstrap</a> provides views styled
|
3273
|
-
// with [Twitter Bootstrap](http://twitter.github.com/bootstrap/) classes.
|
3274
|
-
|
3275
|
-
// Tableling.Modular
|
3276
|
-
// -----------------
|
3277
|
-
//
|
3278
|
-
// Tableling subclass which splits functionality into *modules*
|
3279
|
-
// and handles rendering.
|
3280
|
-
Tableling.Modular = Tableling.Table.extend({
|
3281
|
-
|
3282
|
-
// The list of module names must be specified by subclasses.
|
3283
|
-
modules : [],
|
3284
|
-
|
3285
|
-
// Modules are set up after rendering, before refreshing.
|
3286
|
-
setup : function() {
|
3287
|
-
|
3288
|
-
this.moduleViews = {};
|
3289
|
-
_.each(this.modules, _.bind(this.setupModule, this));
|
3290
|
-
|
3291
|
-
Tableling.Table.prototype.setup.call(this);
|
3292
|
-
},
|
3293
|
-
|
3294
|
-
// ### Modules
|
3295
|
-
// Each module is identified by a name, for example `pageSize`.
|
3296
|
-
setupModule : function(name) {
|
3297
|
-
|
3298
|
-
// The layout must have a region named after the module, e.g. `pageSizeRegion`.
|
3299
|
-
var region = name + 'Region';
|
3300
|
-
|
3301
|
-
// It must have a view class, e.g. `pageSizeView`, which will be shown into
|
3302
|
-
// the region.
|
3303
|
-
var viewClass = this[name + 'View'];
|
3304
|
-
|
3305
|
-
// When instantiated, the view class will be passed the event
|
3306
|
-
// aggregator as the `vent` option. Additional options can be
|
3307
|
-
// given named after the view class, e.g. `pageSizeViewOptions`.
|
3308
|
-
var options = _.extend(this[name + 'ViewOptions'] || {}, { vent: this.vent });
|
3309
|
-
|
3310
|
-
var view = new viewClass(options);
|
3311
|
-
|
3312
|
-
// Module view instances are stored by name in the `moduleViews` property
|
3313
|
-
// for future reference.
|
3314
|
-
this.moduleViews[name] = view;
|
3315
|
-
|
3316
|
-
this[region].show(view);
|
3317
|
-
return view;
|
3318
|
-
},
|
3319
|
-
|
3320
|
-
// By default, a modular table expects a `table` module which
|
3321
|
-
// should have a collection (e.g. a Marionette CompositeView or
|
3322
|
-
// CollectionView). If your subclass does not have that, it
|
3323
|
-
// should override this method to return the Backbone.Collection
|
3324
|
-
// used to fetch table data.
|
3325
|
-
getCollection : function() {
|
3326
|
-
return this.moduleViews.table.collection;
|
3327
|
-
}
|
3328
|
-
});
|
3329
|
-
|
3330
|
-
// ### Example
|
3331
|
-
// This is how a `PageSizeView` module might be registered in a subclass:
|
3332
|
-
//
|
3333
|
-
// var MyTable = Tableling.Modular.extend({
|
3334
|
-
//
|
3335
|
-
// modules : [ 'pageSize' ],
|
3336
|
-
//
|
3337
|
-
// pageSizeView : PageSizeView,
|
3338
|
-
// pageSizeViewOptions : {
|
3339
|
-
// itemView : PageSizeItem
|
3340
|
-
// },
|
3341
|
-
//
|
3342
|
-
// regions : {
|
3343
|
-
// pageSizeRegion : '.pageSize'
|
3344
|
-
// }
|
3345
|
-
// });
|
3346
|
-
|
3347
|
-
// Tableling.Module
|
3348
|
-
// ----------------
|
3349
|
-
//
|
3350
|
-
// A module is an item view that is automatically bound to the table's
|
3351
|
-
// event aggregator.
|
3352
|
-
Tableling.Module = Backbone.Marionette.ItemView.extend({
|
3353
|
-
|
3354
|
-
initialize : function(options) {
|
3355
|
-
this.vent = options.vent;
|
3356
|
-
|
3357
|
-
// The `refresh` method of the view is called once the view is rendered
|
3358
|
-
// and every time the table is refreshed.
|
3359
|
-
this.vent.on('table:refreshed', this.refresh, this);
|
3360
|
-
this.on('render', this.refresh, this);
|
3361
|
-
},
|
3362
|
-
|
3363
|
-
// Call `update` to trigger an update of the table.
|
3364
|
-
update : function() {
|
3365
|
-
this.vent.trigger('table:update', this.config());
|
3366
|
-
},
|
3367
|
-
|
3368
|
-
// Implementations should override this to stay up to date with
|
3369
|
-
// the table state. Note that the data parameter will be undefined
|
3370
|
-
// on the first refresh when the view is rendered.
|
3371
|
-
refresh : function(data) {
|
3372
|
-
},
|
3373
|
-
|
3374
|
-
// New table configuration to be sent on updates. For example,
|
3375
|
-
// a page size view might update the `pageSize` property.
|
3376
|
-
config : function() {
|
3377
|
-
return {};
|
3378
|
-
}
|
3379
|
-
});
|
3380
|
-
|
3381
|
-
// Tableling.FieldModule
|
3382
|
-
// ---------------------
|
3383
|
-
//
|
3384
|
-
// A basic module with a single form field. It comes with sensible
|
3385
|
-
// defaults and only requires a `name` and a `template` parameter.
|
3386
|
-
Tableling.FieldModule = Tableling.Module.extend({
|
3387
|
-
|
3388
|
-
initialize : function(options) {
|
3389
|
-
|
3390
|
-
Tableling.Module.prototype.initialize.call(this, options);
|
3391
|
-
|
3392
|
-
if (!this.ui) {
|
3393
|
-
this.ui = {};
|
3394
|
-
}
|
3395
|
-
// The name attribute of the form field is the same as the
|
3396
|
-
// module's, e.g. `pageSize`.
|
3397
|
-
this.ui.field = '[name="' + this.name + '"]';
|
3398
|
-
|
3399
|
-
if (!this.events) {
|
3400
|
-
this.events = {};
|
3401
|
-
}
|
3402
|
-
this.events['change [name="' + this.name + '"]'] = 'update';
|
3403
|
-
},
|
3404
|
-
|
3405
|
-
// The table property updated is the one with the same name as the module.
|
3406
|
-
config : function() {
|
3407
|
-
var config = {};
|
3408
|
-
config[this.name] = this.ui.field.val();
|
3409
|
-
return config;
|
3410
|
-
}
|
3411
|
-
});
|
3412
|
-
|
3413
|
-
// This is how a `PageSizeView` module might be implemented:
|
3414
|
-
//
|
3415
|
-
// var html = '<input type="text" name="pageSize" />';
|
3416
|
-
//
|
3417
|
-
// var PageSizeView = Tableling.FieldModule.extend({
|
3418
|
-
// name : 'pageSize'
|
3419
|
-
// template : _.template(html)
|
3420
|
-
// });
|
3421
|
-
//
|
3422
|
-
// When the value of the input field changes, the event aggregator will
|
3423
|
-
// receive a `tableling:update` event with the `pageSize` property set
|
3424
|
-
// to that value.
|
3425
|
-
|
3426
|
-
Tableling.Plain = {};
|
3427
|
-
|
3428
|
-
Tableling.Plain.Table = Tableling.Modular.extend({
|
3429
|
-
|
3430
|
-
className: 'tableling',
|
3431
|
-
modules : [ 'table', 'pageSize', 'quickSearch', 'info', 'pagination' ],
|
3432
|
-
template : _.template('<div class="header"><div class="pageSize" /><div class="quickSearch" /></div><div class="table" /><div class="footer"><div class="info" /><div class="pagination" /></div>'),
|
3433
|
-
|
3434
|
-
regions : {
|
3435
|
-
tableRegion : '.table',
|
3436
|
-
pageSizeRegion : '.pageSize',
|
3437
|
-
quickSearchRegion : '.quickSearch',
|
3438
|
-
infoRegion : '.info',
|
3439
|
-
paginationRegion : '.pagination'
|
3440
|
-
}
|
3441
|
-
});
|
3442
|
-
|
3443
|
-
Tableling.Plain.TableView = Backbone.Marionette.CompositeView.extend({
|
3444
|
-
|
3445
|
-
events : {
|
3446
|
-
'click thead th' : 'updateSort'
|
3447
|
-
},
|
3448
|
-
|
3449
|
-
initialize : function(options) {
|
3450
|
-
// TODO: add auto-sort
|
3451
|
-
this.vent = options.vent;
|
3452
|
-
this.sort = [];
|
3453
|
-
},
|
3454
|
-
|
3455
|
-
updateSort : function(ev) {
|
3456
|
-
|
3457
|
-
var el = $(ev.currentTarget);
|
3458
|
-
if (!(el.hasClass('sorting') || el.hasClass('sorting-asc') || el.hasClass('sorting-desc'))) {
|
3459
|
-
return;
|
3460
|
-
}
|
3461
|
-
|
3462
|
-
var field = this.fieldName(el);
|
3463
|
-
|
3464
|
-
if (ev.shiftKey || this.sort.length == 1) {
|
3465
|
-
|
3466
|
-
var existing = _.find(this.sort, function(item) {
|
3467
|
-
return item.field == field;
|
3468
|
-
});
|
3469
|
-
|
3470
|
-
if (existing) {
|
3471
|
-
existing.direction = existing.direction == 'asc' ? 'desc' : 'asc';
|
3472
|
-
el.removeClass('sorting sorting-asc sorting-desc');
|
3473
|
-
el.addClass('sorting-' + existing.direction);
|
3474
|
-
return this.vent.trigger('table:update', this.config());
|
3475
|
-
}
|
3476
|
-
}
|
3477
|
-
|
3478
|
-
if (!ev.shiftKey) {
|
3479
|
-
this.sort.length = 0;
|
3480
|
-
this.$el.find('thead th').removeClass('sorting sorting-asc sorting-desc').addClass('sorting');
|
3481
|
-
}
|
3482
|
-
|
3483
|
-
this.sort.push({
|
3484
|
-
field: field,
|
3485
|
-
direction: 'asc'
|
3486
|
-
});
|
3487
|
-
|
3488
|
-
el.removeClass('sorting sorting-asc sorting-desc').addClass('sorting-asc');
|
3489
|
-
|
3490
|
-
this.vent.trigger('table:update', this.config());
|
3491
|
-
},
|
3492
|
-
|
3493
|
-
config : function() {
|
3494
|
-
return {
|
3495
|
-
page : 1,
|
3496
|
-
sort : this.sortConfig()
|
3497
|
-
};
|
3498
|
-
},
|
3499
|
-
|
3500
|
-
sortConfig : function() {
|
3501
|
-
if (!this.sort.length) {
|
3502
|
-
return null;
|
3503
|
-
}
|
3504
|
-
return _.map(this.sort, function(item) {
|
3505
|
-
return item.field + ' ' + item.direction;
|
3506
|
-
});
|
3507
|
-
},
|
3508
|
-
|
3509
|
-
fieldName : function(el) {
|
3510
|
-
return el.data('field') || el.text().toLowerCase();
|
3511
|
-
}
|
3512
|
-
});
|
3513
|
-
|
3514
|
-
Tableling.Plain.PageSizeView = Tableling.Plain.Table.prototype.pageSizeView = Tableling.FieldModule.extend({
|
3515
|
-
// TODO: update current page intelligently
|
3516
|
-
name : 'pageSize',
|
3517
|
-
template : _.template('<select name="pageSize"><option>5</option><option>10</option><option>15</option></select> entries per page')
|
3518
|
-
});
|
3519
|
-
|
3520
|
-
Tableling.Plain.QuickSearchView = Tableling.Plain.Table.prototype.quickSearchView = Tableling.FieldModule.extend({
|
3521
|
-
name : 'quickSearch',
|
3522
|
-
template : _.template('<input type="text" name="quickSearch" placeholder="Quick search..." />')
|
3523
|
-
});
|
3524
|
-
|
3525
|
-
Tableling.Plain.InfoView = Tableling.Plain.Table.prototype.infoView = Tableling.Module.extend({
|
3526
|
-
|
3527
|
-
template : _.template('Showing <span class="first">0</span> to <span class="last">0</span> of <span class="total">0</span> entries'),
|
3528
|
-
|
3529
|
-
ui : {
|
3530
|
-
first: '.first',
|
3531
|
-
last: '.last',
|
3532
|
-
total: '.total'
|
3533
|
-
},
|
3534
|
-
|
3535
|
-
refresh : function(data) {
|
3536
|
-
if (data) {
|
3537
|
-
this.ui.first.text(this.firstRecord(data));
|
3538
|
-
this.ui.last.text(this.lastRecord(data));
|
3539
|
-
this.ui.total.text(data.total);
|
3540
|
-
}
|
3541
|
-
},
|
3542
|
-
|
3543
|
-
firstRecord : function(data) {
|
3544
|
-
return data.length ? (data.page - 1) * data.pageSize + 1 : 0;
|
3545
|
-
},
|
3546
|
-
|
3547
|
-
lastRecord : function(data) {
|
3548
|
-
return data.length ? this.firstRecord(data) + data.length - 1 : 0;
|
3549
|
-
}
|
3550
|
-
});
|
3551
|
-
|
3552
|
-
Tableling.Plain.PaginationView = Tableling.Plain.Table.prototype.paginationView = Tableling.Module.extend({
|
3553
|
-
|
3554
|
-
template : _.template('<div class="pagination"><ul><li class="first"><a href="#"><<</a></li><li class="previous"><a href="#"><</a></li><li class="next"><a href="#">></a></li><li class="last"><a href="#">>></a></li></ul></div>'),
|
3555
|
-
|
3556
|
-
ui : {
|
3557
|
-
first : '.first',
|
3558
|
-
previous : '.previous',
|
3559
|
-
next : '.next',
|
3560
|
-
last : '.last'
|
3561
|
-
},
|
3562
|
-
|
3563
|
-
events : {
|
3564
|
-
'click .first:not(.disabled)' : 'goToFirstPage',
|
3565
|
-
'click .previous:not(.disabled)' : 'goToPreviousPage',
|
3566
|
-
'click .next:not(.disabled)' : 'goToNextPage',
|
3567
|
-
'click .last:not(.disabled)' : 'goToLastPage'
|
3568
|
-
},
|
3569
|
-
|
3570
|
-
refresh : function(data) {
|
3571
|
-
if (!data) {
|
3572
|
-
this.ui.first.addClass('disabled');
|
3573
|
-
this.ui.previous.addClass('disabled');
|
3574
|
-
this.ui.next.addClass('disabled');
|
3575
|
-
this.ui.last.addClass('disabled');
|
3576
|
-
} else {
|
3577
|
-
this.data = data;
|
3578
|
-
this.enable(this.ui.first, data.page > 1);
|
3579
|
-
this.enable(this.ui.previous, data.page > 1);
|
3580
|
-
this.enable(this.ui.next, data.page < this.numberOfPages(data));
|
3581
|
-
this.enable(this.ui.last, data.page < this.numberOfPages(data));
|
3582
|
-
}
|
3583
|
-
},
|
3584
|
-
|
3585
|
-
enable : function(el, enabled) {
|
3586
|
-
el.removeClass('disabled');
|
3587
|
-
if (!enabled) {
|
3588
|
-
el.addClass('disabled');
|
3589
|
-
}
|
3590
|
-
},
|
3591
|
-
|
3592
|
-
numberOfPages : function() {
|
3593
|
-
return Math.ceil(this.data.total / this.data.pageSize);
|
3594
|
-
},
|
3595
|
-
|
3596
|
-
goToFirstPage : function() {
|
3597
|
-
this.goToPage(1);
|
3598
|
-
},
|
3599
|
-
|
3600
|
-
goToPreviousPage : function() {
|
3601
|
-
this.goToPage(this.data.page - 1);
|
3602
|
-
},
|
3603
|
-
|
3604
|
-
goToNextPage : function() {
|
3605
|
-
this.goToPage(this.data.page + 1);
|
3606
|
-
},
|
3607
|
-
|
3608
|
-
goToLastPage : function() {
|
3609
|
-
this.goToPage(this.numberOfPages());
|
3610
|
-
},
|
3611
|
-
|
3612
|
-
goToPage : function(n) {
|
3613
|
-
this.vent.trigger('table:update', { page : n });
|
3614
|
-
}
|
3615
|
-
});
|
3616
|
-
|
3617
|
-
Tableling.Bootstrap = {};
|
3618
|
-
|
3619
|
-
Tableling.Bootstrap.Table = Tableling.Plain.Table.extend({
|
3620
|
-
template : _.template('<div class="header"><div class="pageSize pull-left" /><div class="quickSearch pull-right" /></div><div class="table" /><div class="footer"><div class="info pull-left" /><div class="pagination pull-right" /></div>')
|
3621
|
-
});
|
3622
|
-
|
3623
|
-
Tableling.Bootstrap.TableView = Tableling.Plain.TableView.extend({});
|
3624
|
-
|
3625
|
-
Tableling.Bootstrap.PageSizeView = Tableling.Bootstrap.Table.prototype.pageSizeView = Tableling.Plain.PageSizeView.extend({
|
3626
|
-
template : _.template('<select name="pageSize" class="input-mini"><option>5</option><option>10</option><option>15</option></select> entries per page')
|
3627
|
-
});
|
3628
|
-
|
3629
|
-
Tableling.Bootstrap.QuickSearchView = Tableling.Bootstrap.Table.prototype.quickSearchView = Tableling.Plain.QuickSearchView.extend({});
|
3630
|
-
|
3631
|
-
Tableling.Bootstrap.InfoView = Tableling.Bootstrap.Table.prototype.infoView = Tableling.Plain.InfoView.extend({});
|
3632
|
-
|
3633
|
-
Tableling.Bootstrap.PaginationView = Tableling.Bootstrap.Table.prototype.paginationView = Tableling.Plain.PaginationView.extend({});
|
3634
|
-
|
3635
|
-
|
3636
|
-
return Tableling;
|
3637
|
-
|
3638
|
-
})(Backbone, _, $ || window.jQuery || window.Zepto || window.ender);
|