/
infobox-view.js
executable file
·71 lines (58 loc) · 1.74 KB
/
infobox-view.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
var CoreView = require('backbone/core-view');
var checkAndBuildOpts = require('builder/helpers/required-opts');
var REQUIRED_OPTS = [
'infoboxModel',
'infoboxCollection'
];
module.exports = CoreView.extend({
initialize: function (opts) {
checkAndBuildOpts(opts, REQUIRED_OPTS, this);
this._initBinds();
this._setSelectedModel();
},
render: function () {
this.clearSubViews();
this.$el.empty();
this._initViews();
return this;
},
_initBinds: function () {
this.listenTo(this._infoboxModel, 'change:state', this._onChangeState);
this.listenTo(this._infoboxModel, 'change:visible', this.render);
},
_initViews: function () {
var createContentView;
if (this._selectedModel) {
createContentView = this._selectedModel.get('createContentView');
this.infoboxView = createContentView();
this.$el.append(this.infoboxView.render().el);
this.addView(this.infoboxView);
this._initSubviewBinds();
}
},
_setSelectedModel: function () {
var state = this._infoboxModel.get('state');
this._selectedModel = this._infoboxCollection.setSelected(state);
},
_onChangeState: function () {
this._setSelectedModel();
this.render();
},
_initSubviewBinds: function () {
this.listenTo(this.infoboxView, 'action:main', this._onAction);
this.listenTo(this.infoboxView, 'action:close', this._onClose);
},
_onAction: function () {
var action = this._selectedModel.get('onAction');
action && action();
},
_onClose: function () {
var action = this._selectedModel.get('onClose');
if (action) {
action();
} else {
// if no action associated, we still need to hide the infobox
this._infoboxModel.set('state', null);
}
}
});