backbone-way
Rock your page in Backbone way!
by Pinky Jie
HTML
<link rel="stylesheet" href="http://libs.baidu.com/bootstrap/2.0.4/css/bootstrap.min.css">
<script src="http://libs.baidu.com/underscore/1.3.3/underscore-min.js"></script>
<div id="content" class="container">
<h3>Users Page</h3>
<hr>
<div id="user-list">
<div class="header">
<span>All users</span>
<a href="#" title="add">
<span class="pull-right"><i class="icon-plus"></i>Add user</span>
</a>
</div>
<table class="table table-bordered">
<thead>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Action</th>
</thead>
<tbody></tbody>
</table>
</div>
<hr>
<div id="edit-form">
</div>
</div>
<script type="text/template" id="item-template">
<tr>
<td><%- id %></td>
<td><%- name %></td>
<td><%- gender %></td>
<td><%- age %></td>
<td>
<a href="#" title="edit"><i class="icon-edit"></i></a>
<a href="#" title="remove"><i class="icon-remove"></i></a>
</td>
</tr>
</script>
<script type="text/template" id="form-template">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="input-name">Name:</label>
<div class="controls">
<input type="text" id="input-name" name="name" placeholder="Name" value="<%- name %>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="input-gender">Gender:</label>
<div class="controls">
<label class="radio inline">
<input type="radio" name="gender"...
CSS
#content {
width: 400px;
}
.header {
margin-bottom: 10px;
}
.header span {
font-size: 12pt;
}
.text-center {
text-align: center;
}
#user-list td, #user-list th {
text-align: center;
}
JavaScript
(function($){
// view for table div
var TableView = function(element, template, sync) {
this.$el = $(element);
this.itemTemplate = _.template($(template).html());
this.sync = sync;
this.collections = [];
// reference sub element
this.$tableBody = $('tbody', this.$el);
this.$addBtn = $('a[title=add]', this.$el);
// bind events
_.bindAll(this, 'addItem', 'editItem', 'removeItem', 'onFormSaved');
this.$addBtn.on('click', this.addItem);
this.$el.on('click', 'a[title=edit]', this.editItem);
this.$el.on('click', 'a[title=remove]', this.removeItem);
// listen to events from other view
EventBus.on('FormView:saveForm', this.onFormSaved);
};
TableView.prototype = {
render: function() {
this.reset();
var self = this;
this.sync.fetch(function(response){
response.users.forEach(function(item){
self.$tableBody.append(self.itemTemplate(item));
self.collections.push(item);
});
});
},
reset: function() {
this.$tableBody.empty();
this.collections = [];
},
addItem: function(e) {
e.preventDefault();
var nullUser = {
name: '',
gender: 'boy',
age: 0
};
EventBus.trigger('TableView:addItem', nullUser);
},
editItem: function(e) {
e.preventDefault();
var itemId = +$(e.target).parents('tr').children('td:eq(0)').text();
EventBus.trigger('TableView:editItem', this._findModel(itemId));
},
removeItem: function(e) {
e.preventDefault();
var $tr = $(e.target).parents('tr');
var itemId = +$tr.children('td:eq(0)').text();
var self = this;
this.sync.destroy(itemId,...