Skip to content

Commit d623ff0

Browse files
committed
Merge pull request #1 from coding-kata/refactoring-to-backbone
migrate jquery to backbone
2 parents 4df6037 + 1b229f7 commit d623ff0

File tree

11 files changed

+3406
-34
lines changed

11 files changed

+3406
-34
lines changed

app.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<meta charset="utf-8">
55
<title>Sample</title>
66
<script src="./vendor/jquery-1.11.2.min.js"></script>
7-
<script src="app.js"></script>
7+
<script src="./vendor/underscore.js"></script>
8+
<script src="./vendor/backbone.js"></script>
9+
<script src="lib/bundle.js"></script>
810
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
911
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
1012
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans">

lib/bundle.js

Lines changed: 269 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
},
1010
"main": "index.js",
1111
"scripts": {
12-
"start":"http-server",
12+
"start": "http-server",
13+
"build": "browserify src/app.js -t 6to5ify -o lib/bundle.js",
14+
"watch": "watchify src/app.js -d -t 6to5ify -o lib/bundle.js",
1315
"test": "mocha test/*.js"
1416
},
1517
"directories": {
@@ -21,12 +23,15 @@
2123
"url": "https://github.com/coding-kata/todo-app-jquery-to-backbone/issues"
2224
},
2325
"devDependencies": {
26+
"6to5ify": "^3.1.2",
27+
"browserify": "^8.1.1",
2428
"espower-loader": "^0.10.0",
2529
"http-server": "^0.7.4",
2630
"intelli-espower-loader": "^0.6.0",
2731
"mocha": "^2.1.0",
2832
"phantomjs": "^1.9.13",
2933
"power-assert": "^0.10.1",
30-
"testium": "^2.3.0"
34+
"testium": "^2.3.0",
35+
"watchify": "^2.2.1"
3136
}
3237
}

src/app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import TodoItemView from "./todo-item-view.js"
2+
import TodoItem from "./todo-item-model.js"
3+
import TodoItemList from "./todo-item-collection.js"
4+
5+
$(function () {
6+
var $form = $('.todoForm');
7+
var $list = $('.todoList');
8+
var todoItemList = new TodoItemList();
9+
todoItemList.on("add", function (todoItem) {
10+
var item = new TodoItemView({model: todoItem});
11+
var list = item.render().el;
12+
$list.append(list);
13+
});
14+
15+
function createTodoItem(text) {
16+
todoItemList.add({
17+
title: text
18+
});
19+
}
20+
21+
$form.on('submit', function (e) {
22+
e.preventDefault();
23+
24+
var $input = $('input[type="text"]');
25+
var val = $input.val();
26+
createTodoItem(val);
27+
28+
$input.val('');
29+
});
30+
});

src/todo-item-collection.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
var {Collection} = Backbone;
4+
import TodoItem from "./todo-item-model.js"
5+
class TodoItemList extends Collection {
6+
constructor(options) {
7+
super(options);
8+
this.model = TodoItem;
9+
}
10+
}
11+
export default TodoItemList;

src/todo-item-model.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
var {Model} = Backbone;
4+
class TodoItem extends Model {
5+
defaults() {
6+
return {
7+
title: '',
8+
completed: false
9+
};
10+
}
11+
12+
toggle() {
13+
this.save({
14+
completed: !this.get('completed')
15+
});
16+
}
17+
}
18+
export default TodoItem;

src/todo-item-view.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
var {View} = Backbone;
4+
class TodoItemView extends View {
5+
constructor(options) {
6+
this.tagName = 'li';
7+
8+
// *Cache the template function for a single item.*
9+
this.template = _.template(`
10+
<input type="checkbox" class="<%= completed ? 'is-complete' : '' %>" <%= completed ? 'checked' : '' %>>
11+
<span class="todoText"><%- title %></span>
12+
<i class="removeBtn fa fa-times"></i>
13+
`);
14+
// *Define the DOM events specific to an item.*
15+
this.events = {
16+
'click input': this.toggleComplete,
17+
'click .removeBtn': this.removeItem
18+
};
19+
super(options);
20+
21+
this.listenTo(this.model, 'change', this.render);
22+
this.listenTo(this.model, 'destroy', this.remove);
23+
}
24+
25+
// *Re-render the contents of the todo item.*
26+
render() {
27+
this.$el.html(this.template(this.model.toJSON()));
28+
this.$el.toggleClass('completed', this.model.get('completed'));
29+
return this;
30+
}
31+
32+
toggleComplete() {
33+
this.model.toggle();
34+
}
35+
36+
removeItem() {
37+
if (!window.confirm('消しますよ')) {
38+
return;
39+
}
40+
this.model.destroy();
41+
return this;
42+
}
43+
}
44+
export default TodoItemView;

test/app-e2e-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function addTodo(text) {
88
browser.click('.todoBtn');
99
}
1010
describe("app-test", function () {
11-
var text = 'todo test';
11+
var text = 'todo text';
1212
before(injectBrowser());
1313
beforeEach(function () {
1414
browser = this.browser;

0 commit comments

Comments
 (0)