-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.html
174 lines (145 loc) · 5.46 KB
/
backbone.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Backbone.js Flux Example</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.component {
border: 1px dotted red;
margin: 5px 0;
padding: 5px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>
<script src="../src/flux.backbone.js"></script>
<script>
/*global Flux*/
var ADD_PERSON = 'ADD_PERSON';
var STORE_UPDATED = 'STORE_UPDATED';
window.FluxForBackbone = {
Models: {},
Collections: {},
Views: {},
Routers: {},
Stores: {},
Dispatcher: new Flux.Dispatcher(),
init: function() {
'use strict';
var router = new FluxForBackbone.Routers.Main();
var started = Backbone.history.start({
pushState: false
});
if (!started) {
throw 'Failed';
}
}
};
$(document).ready(function() {
'use strict';
FluxForBackbone.Models.Person = Backbone.Model.extend();
FluxForBackbone.Stores.Persons = new Flux.Store({
initialize: function() {
var store = this;
// notify this store's listeners on updates
store.items.on('all', function() {
store.emit(new Flux.Action(STORE_UPDATED));
});
// listen for dispatcher, whenever person added event is triggered
FluxForBackbone.Dispatcher.on(ADD_PERSON, function(name) {
store.addItem(new FluxForBackbone.Models.Person({name: name}));
})
}
});
FluxForBackbone.Views.Form = Backbone.View.extend({
template: _.template($('#form-template').html()),
el: $('#form-container'),
events: {
'click button#add-person': 'addPerson',
},
initialize: function() {
},
addPerson: function() {
FluxForBackbone.Dispatcher.trigger(ADD_PERSON, this.$el.find('#person-name').val());
},
render: function() {
this.$el.html(this.template());
return this;
}
});
FluxForBackbone.Views.Result = Backbone.View.extend({
template: _.template('Persons added: <%= persons %>'),
el: $('#result-container'),
initialize: function() {
var that = this;
FluxForBackbone.Stores.Persons.addListener(function(action) {
switch (action.type) {
case STORE_UPDATED:
that.render();
break;
}
});
},
render: function() {
this.$el.html(this.template({
persons: JSON.stringify(FluxForBackbone.Stores.Persons.getItems())
}));
return this;
}
});
FluxForBackbone.Views.Count = Backbone.View.extend({
template: _.template($('#count-template').html()),
el: $('#count-container'),
initialize: function() {
var that = this;
FluxForBackbone.Stores.Persons.addListener(function(action) {
switch (action.type) {
case STORE_UPDATED:
that.render();
break;
}
});
},
render: function() {
this.$el.html(this.template({
count: FluxForBackbone.Stores.Persons.getItems().length
}));
return this;
}
});
FluxForBackbone.Routers.Main = Backbone.Router.extend({
routes: {
'': 'frontpage'
},
frontpage: function() {
var formView = new FluxForBackbone.Views.Form();
var resultView = new FluxForBackbone.Views.Result();
var countView = new FluxForBackbone.Views.Count();
formView.render();
resultView.render();
countView.render();
},
});
FluxForBackbone.init();
});
</script>
</head>
<body>
<script type="text/template" id="form-template">
<label for="person-name">Name</label><input name="name" type="text" id="person-name">
<button id="add-person">
Add person!
</button>
</script>
<script type="text/template" id="count-template">
Total persons: <%= count %>
</script>
<div id="form-container" class="component"></div>
<div id="result-container" class="component"></div>
<div id="count-container" class="component"></div>
</body>
</html>