Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
71 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
var router = require('express').Router(); | ||
var AV = require('leanengine'); | ||
|
||
// `AV.Object.extend` 方法一定要放在全局变量,否则会造成堆栈溢出。 | ||
// 详见: https://leancloud.cn/docs/js_guide.html#对象 | ||
var Todo = AV.Object.extend('Todo'); | ||
|
||
// 查询 Todo 列表 | ||
router.get('/', function(req, res, next) { | ||
var query = new AV.Query(Todo); | ||
query.descending('createdAt'); | ||
query.find({ | ||
success: function(results) { | ||
res.render('todos', { | ||
title: 'TODO 列表', | ||
todos: results | ||
}); | ||
}, | ||
error: function(err) { | ||
if (err.code === 101) { | ||
// 该错误的信息为:{ code: 101, message: 'Class or object doesn\'t exists.' },说明 Todo 数据表还未创建,所以返回空的 Todo 列表。 | ||
// 具体的错误代码详见:https://leancloud.cn/docs/error_code.html | ||
res.render('todos', { | ||
title: 'TODO 列表', | ||
todos: [] | ||
}); | ||
} else { | ||
next(err); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// 新增 Todo 项目 | ||
router.post('/', function(req, res, next) { | ||
var content = req.body.content; | ||
var todo = new Todo(); | ||
todo.set('content', content); | ||
todo.save(null, { | ||
success: function(todo) { | ||
res.redirect('/todos'); | ||
}, | ||
error: function(err) { | ||
next(err); | ||
} | ||
}) | ||
}) | ||
|
||
module.exports = router; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>Todo</title> | ||
<link rel="stylesheet" href="/stylesheets/style.css"> | ||
</head> | ||
<body> | ||
<h1><%= title %></h1> | ||
<form action="/todos" method="POST"> | ||
<input type="text" name="content" /> | ||
<input type="submit" value="新增" /> | ||
</form> | ||
<ul> | ||
<% for(var i=0; i<todos.length; i++) {%> | ||
<li><%= todos[i].get('content') %></li> | ||
<% } %> | ||
</ul> | ||
</body> | ||
</html> |