Skip to content

migrate jquery to backbone #1

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

Merged
merged 10 commits into from
Jan 21, 2015
Merged
30 changes: 0 additions & 30 deletions app.js

This file was deleted.

4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<meta charset="utf-8">
<title>Sample</title>
<script src="./vendor/jquery-1.11.2.min.js"></script>
<script src="app.js"></script>
<script src="./vendor/underscore.js"></script>
<script src="./vendor/backbone.js"></script>
<script src="lib/bundle.js"></script>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans">
Expand Down
269 changes: 269 additions & 0 deletions lib/bundle.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
},
"main": "index.js",
"scripts": {
"start":"http-server",
"start": "http-server",
"build": "browserify src/app.js -t 6to5ify -o lib/bundle.js",
"watch": "watchify src/app.js -d -t 6to5ify -o lib/bundle.js",
"test": "mocha test/*.js"
},
"directories": {
Expand All @@ -21,12 +23,15 @@
"url": "https://github.com/coding-kata/todo-app-jquery-to-backbone/issues"
},
"devDependencies": {
"6to5ify": "^3.1.2",
"browserify": "^8.1.1",
"espower-loader": "^0.10.0",
"http-server": "^0.7.4",
"intelli-espower-loader": "^0.6.0",
"mocha": "^2.1.0",
"phantomjs": "^1.9.13",
"power-assert": "^0.10.1",
"testium": "^2.3.0"
"testium": "^2.3.0",
"watchify": "^2.2.1"
}
}
30 changes: 30 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import TodoItemView from "./todo-item-view.js"
import TodoItem from "./todo-item-model.js"
import TodoItemList from "./todo-item-collection.js"

$(function () {
var $form = $('.todoForm');
var $list = $('.todoList');
var todoItemList = new TodoItemList();
todoItemList.on("add", function (todoItem) {
var item = new TodoItemView({model: todoItem});
var list = item.render().el;
$list.append(list);
});

function createTodoItem(text) {
todoItemList.add({
title: text
});
}

$form.on('submit', function (e) {
e.preventDefault();

var $input = $('input[type="text"]');
var val = $input.val();
createTodoItem(val);

$input.val('');
});
});
11 changes: 11 additions & 0 deletions src/todo-item-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// LICENSE : MIT
"use strict";
var {Collection} = Backbone;
import TodoItem from "./todo-item-model.js"
class TodoItemList extends Collection {
constructor(options) {
super(options);
this.model = TodoItem;
}
}
export default TodoItemList;
18 changes: 18 additions & 0 deletions src/todo-item-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// LICENSE : MIT
"use strict";
var {Model} = Backbone;
class TodoItem extends Model {
defaults() {
return {
title: '',
completed: false
};
}

toggle() {
this.save({
completed: !this.get('completed')
});
}
}
export default TodoItem;
44 changes: 44 additions & 0 deletions src/todo-item-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// LICENSE : MIT
"use strict";
var {View} = Backbone;
class TodoItemView extends View {
constructor(options) {
this.tagName = 'li';

// *Cache the template function for a single item.*
this.template = _.template(`
<input type="checkbox" class="<%= completed ? 'is-complete' : '' %>" <%= completed ? 'checked' : '' %>>
<span class="todoText"><%- title %></span>
<i class="removeBtn fa fa-times"></i>
`);
// *Define the DOM events specific to an item.*
this.events = {
'click input': this.toggleComplete,
'click .removeBtn': this.removeItem
};
super(options);

this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
}

// *Re-render the contents of the todo item.*
render() {
this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('completed', this.model.get('completed'));
return this;
}

toggleComplete() {
this.model.toggle();
}

removeItem() {
if (!window.confirm('消しますよ')) {
return;
}
this.model.destroy();
return this;
}
}
export default TodoItemView;
2 changes: 1 addition & 1 deletion test/app-e2e-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function addTodo(text) {
browser.click('.todoBtn');
}
describe("app-test", function () {
var text = 'todo test';
var text = 'todo text';
before(injectBrowser());
beforeEach(function () {
browser = this.browser;
Expand Down
Loading