/
derby.browser.js
69 lines (61 loc) · 2.07 KB
/
derby.browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var racer = require('racer')
, tracks = require('tracks')
, derbyModel = require('./derby.Model')
, Dom = require('./Dom')
, View = require('./View')
, autoRefresh = require('./refresh').autoRefresh;
exports.createApp = createApp;
function createApp(appModule) {
var appExports = appModule.exports
, view, model;
appModule.exports = function(modelBundle, appHash, debug, ns, ctx) {
tracks.set('debug', debug);
// The init event is fired after the model data is initialized but
// before the socket object is set
racer.on('init', function(_model) {
model = view.model = _model;
var dom = view.dom = new Dom(model, appExports);
derbyModel.init(model, dom, view);
// Ignore errors thrown when rendering; these will also be thrown
// on the server, and throwing here causes the app not to connect
try {
// Render immediately upon initialization so that the page is in
// the same state it was when rendered on the server
view.render(model, ns, ctx, true);
} catch (err) {}
});
// The ready event is fired after the model data is initialized and
// the socket object is set
if (debug) {
racer.on('ready', function(model) {
autoRefresh(view, model, appHash);
});
}
racer.init(modelBundle);
return appExports;
};
// Expose methods on the application module. Note that view must added
// to both appModule.exports and appExports, since it is used before
// the initialization function to make templates
appModule.exports.view = appExports.view = view = new View;
function createPage() {
return {
render: function(ns, ctx) {
view.render(model, ns, ctx);
}
};
}
function onRoute(callback, page, params, next, isTransitional) {
if (isTransitional) {
callback(model, params, next);
} else {
callback(page, model, params, next);
}
}
tracks.setup(appExports, createPage, onRoute);
view.history = appExports.history;
appExports.ready = function(fn) {
racer.on('ready', fn);
};
return appExports;
};