Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructured index files to match video tutorial #83

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 2 additions & 166 deletions videos/beginner/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,170 +6,6 @@
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
</head>
<body>


<div class="container">
<h1>User Manager</h1>
<hr />
<div class="page"></div>
</div>


<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>

<script type="text/template" id="user-list-template">
<a href="#/new" class="btn btn-primary">New</a>
<hr />
<table class="table striped">
<thead>
<tr>
<th>First Name</th><th>Last Name</th><th>Age</th><th></th>
</tr>
</thead>
<tbody>
<% _.each(users, function(user) { %>
<tr>
<td><%= htmlEncode(user.get('firstname')) %></td>
<td><%= htmlEncode(user.get('lastname')) %></td>
<td><%= htmlEncode(user.get('age')) %></td>
<td><a class="btn" href="#/edit/<%= user.id %>">Edit</a></td>
</tr>
<% }); %>
</tbody>
</table>
</script>

<script type="text/template" id="edit-user-template">
<form class="edit-user-form">
<legend><%= user ? 'Edit' : 'New' %> User</legend>
<label>First Name</label>
<input name="firstname" type="text" value="<%= user ? user.get('firstname') : '' %>">
<label>Last Name</label>
<input name="lastname" type="text" value="<%= user ? user.get('lastname') : '' %>">
<label>Age</label>
<input name="age" type="text" value="<%= user ? user.get('age') : '' %>">
<hr />
<button type="submit" class="btn"><%= user ? 'Update' : 'Create' %></button>
<% if(user) { %>
<input type="hidden" name="id" value="<%= user.id %>" />
<button data-user-id="<%= user.id %>" class="btn btn-danger delete">Delete</button>
<% }; %>
</form>
</script>

<script>
function htmlEncode(value){
return $('<div/>').text(value).html();
}
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url;
});

var Users = Backbone.Collection.extend({
url: '/users'
});

var User = Backbone.Model.extend({
urlRoot: '/users'
});

var UserListView = Backbone.View.extend({
el: '.page',
render: function () {
var that = this;
var users = new Users();
users.fetch({
success: function (users) {
var template = _.template($('#user-list-template').html(), {users: users.models});
that.$el.html(template);
}
})
}
});

var userListView = new UserListView();

var UserEditView = Backbone.View.extend({
el: '.page',
events: {
'submit .edit-user-form': 'saveUser',
'click .delete': 'deleteUser'
},
saveUser: function (ev) {
var userDetails = $(ev.currentTarget).serializeObject();
var user = new User();
user.save(userDetails, {
success: function (user) {
router.navigate('', {trigger:true});
}
});
return false;
},
deleteUser: function (ev) {
this.user.destroy({
success: function () {
console.log('destroyed');
router.navigate('', {trigger:true});
}
})
},
render: function (options) {
var that = this;
if(options.id) {
that.user = new User({id: options.id});
that.user.fetch({
success: function (user) {
var template = _.template($('#edit-user-template').html(), {user: user});
that.$el.html(template);
}
})
} else {
var template = _.template($('#edit-user-template').html(), {user: null});
that.$el.html(template);
}
}
});

var userEditView = new UserEditView();

var Router = Backbone.Router.extend({
routes: {
"": "home",
"edit/:id": "edit",
"new": "edit",
}
});

var router = new Router;
router.on('route:home', function() {
// render user list
userListView.render();
})
router.on('route:edit', function(id) {
userEditView.render({id: id});
})
Backbone.history.start();
</script>



</body>
</html>

</html>
175 changes: 175 additions & 0 deletions videos/beginner/index_completed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>BackboneTutorials.com Beginner Video</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
</head>
<body>


<div class="container">
<h1>User Manager</h1>
<hr />
<div class="page"></div>
</div>


<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>

<script type="text/template" id="user-list-template">
<a href="#/new" class="btn btn-primary">New</a>
<hr />
<table class="table striped">
<thead>
<tr>
<th>First Name</th><th>Last Name</th><th>Age</th><th></th>
</tr>
</thead>
<tbody>
<% _.each(users, function(user) { %>
<tr>
<td><%= htmlEncode(user.get('firstname')) %></td>
<td><%= htmlEncode(user.get('lastname')) %></td>
<td><%= htmlEncode(user.get('age')) %></td>
<td><a class="btn" href="#/edit/<%= user.id %>">Edit</a></td>
</tr>
<% }); %>
</tbody>
</table>
</script>

<script type="text/template" id="edit-user-template">
<form class="edit-user-form">
<legend><%= user ? 'Edit' : 'New' %> User</legend>
<label>First Name</label>
<input name="firstname" type="text" value="<%= user ? user.get('firstname') : '' %>">
<label>Last Name</label>
<input name="lastname" type="text" value="<%= user ? user.get('lastname') : '' %>">
<label>Age</label>
<input name="age" type="text" value="<%= user ? user.get('age') : '' %>">
<hr />
<button type="submit" class="btn"><%= user ? 'Update' : 'Create' %></button>
<% if(user) { %>
<input type="hidden" name="id" value="<%= user.id %>" />
<button data-user-id="<%= user.id %>" class="btn btn-danger delete">Delete</button>
<% }; %>
</form>
</script>

<script>
function htmlEncode(value){
return $('<div/>').text(value).html();
}
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url;
});

var Users = Backbone.Collection.extend({
url: '/users'
});

var User = Backbone.Model.extend({
urlRoot: '/users'
});

var UserListView = Backbone.View.extend({
el: '.page',
render: function () {
var that = this;
var users = new Users();
users.fetch({
success: function (users) {
var template = _.template($('#user-list-template').html(), {users: users.models});
that.$el.html(template);
}
})
}
});

var userListView = new UserListView();

var UserEditView = Backbone.View.extend({
el: '.page',
events: {
'submit .edit-user-form': 'saveUser',
'click .delete': 'deleteUser'
},
saveUser: function (ev) {
var userDetails = $(ev.currentTarget).serializeObject();
var user = new User();
user.save(userDetails, {
success: function (user) {
router.navigate('', {trigger:true});
}
});
return false;
},
deleteUser: function (ev) {
this.user.destroy({
success: function () {
console.log('destroyed');
router.navigate('', {trigger:true});
}
})
},
render: function (options) {
var that = this;
if(options.id) {
that.user = new User({id: options.id});
that.user.fetch({
success: function (user) {
var template = _.template($('#edit-user-template').html(), {user: user});
that.$el.html(template);
}
})
} else {
var template = _.template($('#edit-user-template').html(), {user: null});
that.$el.html(template);
}
}
});

var userEditView = new UserEditView();

var Router = Backbone.Router.extend({
routes: {
"": "home",
"edit/:id": "edit",
"new": "edit",
}
});

var router = new Router;
router.on('route:home', function() {
// render user list
userListView.render();
})
router.on('route:edit', function(id) {
userEditView.render({id: id});
})
Backbone.history.start();
</script>


</body>
</html>