forked from massiveart/husky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.js
112 lines (88 loc) · 3.21 KB
/
backbone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* This file is part of Husky frontend development framework.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*
*/
(function() {
'use strict';
if (window.Backbone) {
define('backbone', [], function() {
return window.Backbone;
});
} else {
require.config({
paths: { backbone: 'bower_components/backbone/backbone' },
shim: { backbone: { exports: 'Backbone', deps: ['underscore', 'jquery'] } }
});
}
define(['backbone'], {
name: 'Backbone',
initialize: function(app) {
var core = app.core,
sandbox = app.sandbox,
_ = app.sandbox.util._,
views = {};
core.mvc = require('backbone');
sandbox.mvc = {};
sandbox.mvc.routes = [];
sandbox.mvc.Router = function(options) {
return core.mvc.Router.extend(options);
};
sandbox.mvc.View = function(options) {
return core.mvc.View.extend(options);
};
sandbox.mvc.Model = function(options) {
return core.mvc.Model.extend(options);
};
sandbox.mvc.Collection = function(options) {
return core.mvc.Collection.extend(options);
};
sandbox.mvc.history = core.mvc.history;
define('mvc/view', function() {
return sandbox.mvc.View;
});
define('mvc/model', function() {
return sandbox.mvc.Model;
});
define('mvc/collection', function() {
return sandbox.mvc.Collection;
});
// Injecting a Backbone view in the Component just before initialization.
// This View's class will be built and cached this first time the component is included.
app.components.before('initialize', function(options) {
if (!this) {
throw new Error('Missing context!');
}
// check component needs a view
if (!!this.view) {
var View = views[options.ref],
ext;
if (!View) {
ext = _.pick(this, 'model', 'collection', 'id', 'attributes', 'className', 'tagName', 'events');
views[options.ref] = View = sandbox.mvc.View(ext);
}
this.view = new View({ el: this.$el });
this.view.sandbox = this.sandbox;
this.view.parent = this;
}
});
app.components.before('remove', function() {
if (!this) {
throw new Error('Missing context!');
}
this.view && this.view.stopListening();
});
},
afterAppStart: function(app) {
app.sandbox.util._.delay(function() {
if (!app.core.mvc.History.started) {
app.core.mvc.history.start();
}
}, 250);
}
});
})();