forked from joshelser/Concert
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Widget.js
82 lines (72 loc) · 2.49 KB
/
Widget.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
/**
* @file Widget.js
* @author Colin Sullivan <colinsul [at] gmail.com>
**/
/**
* This contains general stuff that needs to take place for any widget on the UI.
* @class
* @extends Backbone.View
**/
var Widget = Backbone.View.extend(
/**
* @scope Widget.prototype
**/
{
/**
* Create the DOM elements associated with this widget using a template.
* The widget is not inserted into the DOM in this class. That is for whoever
* is instantiating the widget to take care of.
*
* @param {jQuery tmpl object} params.template - The template.
* @param {Panel} params.panel - Panel that we belong to.
**/
initialize: function(params) {
var params = this.options;
var template = params.template;
if(typeof(template) == 'undefined' && typeof(params.el) == 'undefined') {
throw new Error('params.template or params.el must be undefined');
}
else if(typeof(template) != 'undefined' && template.length == 0) {
throw new Error('template not found');
}
this.template = template;
var panel = params.panel;
if(typeof(panel) == 'undefined') {
throw new Error('params.panel is undefined');
}
this.panel = panel;
_.bindAll(this, 'render');
if(this.model) {
this.model.bind('change', this.render);
}
},
render: function() {
var template = this.template;
if(template) {
/* render new widget */
var newel = template.tmpl(this.getTemplateData());
if(newel.length != 1) {
throw new Error('widgets must have a single containing element');
}
var el = this.el;
/* If this element is currently in the DOM */
if($(el).parent().length) {
/* replace old widget in DOM (or non-dom) */
$(el).replaceWith(newel);
}
/* Save reference to new widget */
this.el = newel.get(0);
}
this.delegateEvents();
return this;
},
/**
* Hook for retrieval of data to send to template incase
* some data needs to be determined when template is rendered.
* By default just returns an object with this widget's
* model in it.
**/
getTemplateData: function () {
return this.model;
},
});