-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangles.js
81 lines (67 loc) · 1.25 KB
/
rectangles.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
(function () {
var Rectangle = Backbone.Model.extend({});
var RectangleView = Backbone.View.extend({
tagName: 'div',
className: 'rectangle',
events: {
'click': 'move'
},
render: function () {
this.setDimensions();
this.setPosition();
this.setColor();
return this;
},
setDimensions: function () {
this.$el.css({
width: this.model.get('width') + 'px',
height: this.model.get('height') + 'px'
});
},
setPosition: function () {
var position = this.model.get('position');
this.$el.css({
left: position.x,
top: position.y
});
},
setColor: function () {
this.$el.css('background-color', this.model.get('color'));
},
move: function () {
this.$el.css('left', this.$el.position().left + 10);
}
});
var models = [
new Rectangle({
width: 100,
height: 60,
position: {
x: 300,
y: 150
},
color: '#ff0000'
}),
new Rectangle({
width: 26,
height: 300,
position: {
x: 500,
y: 75
},
color: '#00ff00'
}),
new Rectangle({
width: 300,
height: 70,
position: {
x: 310,
y: 200
},
color: '#0000ff'
})
];
_(models).each(function (model) {
$('div#canvas').append(new RectangleView({model: model}).render().el);
});
})();