Skip to content

Commit 3c7bba5

Browse files
committed
1. Fixed the bug that req.param('id') is null in data adapter by adding express routes.
The routes do nothing by parse the url params. 2. Use `_id` instead of `id` in templates. 3. Now support PORT, GET, and DEL. PUT is going to be added. 4. Bug: After reloading the deleting view, it would not get the model.
1 parent a6a51e5 commit 3c7bba5

File tree

10 files changed

+49
-27
lines changed

10 files changed

+49
-27
lines changed

app/controllers/posts_controller.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
module.exports = {
22
index: function(params, callback) {
3-
var spec = {
3+
var spec, options;
4+
spec = {
45
collection: {collection: 'posts', params: params}
56
};
6-
this.app.fetch(spec, function(err, result) {
7+
options = {
8+
readFromCache: false,
9+
writeToCache: true
10+
}
11+
this.app.fetch(spec, options, function(err, result) {
712
callback(err, 'posts_index_view', result);
813
});
914
},
@@ -17,7 +22,6 @@ module.exports = {
1722
model: {model: 'post', params: params, ensureKeys: ['title']}
1823
};
1924
this.app.fetch(spec, function(err, result) {
20-
console.log(result, 2222);
2125
callback(err, 'posts_show_view', result);
2226
});
2327
},
@@ -36,6 +40,7 @@ module.exports = {
3640
model: {model: 'post', params: params, ensureKeys: ['title']}
3741
};
3842
this.app.fetch(spec, function(err, result) {
43+
console.log(result, 111);
3944
callback(err, 'posts_del_view', result);
4045
});
4146
}

app/models/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ var Base = require('./base');
22

33
module.exports = Base.extend({
44
url: '/posts/:id',
5-
idAttribute: 'id'
5+
idAttribute: '_id'
66
});
77
module.exports.id = 'Post';

app/templates/compiledTemplates.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ helpers = helpers || Handlebars.helpers; data = data || {};
1818

1919

2020
buffer += "<p>Sure to delete?</p>\n<div>\n<button class=\"btn btn-danger del\">Delete it!</button>\n<a href=\"/posts/";
21-
if (stack1 = helpers.id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
22-
else { stack1 = depth0.id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
21+
if (stack1 = helpers._id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
22+
else { stack1 = depth0._id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
2323
buffer += escapeExpression(stack1)
2424
+ "\" class=\"btn\">Cancel</a>\n</div>\n";
2525
return buffer;
@@ -64,8 +64,8 @@ function program1(depth0,data) {
6464

6565
var buffer = "", stack1;
6666
buffer += "\n <li>\n <a href=\"/posts/";
67-
if (stack1 = helpers.id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
68-
else { stack1 = depth0.id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
67+
if (stack1 = helpers._id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
68+
else { stack1 = depth0._id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
6969
buffer += escapeExpression(stack1)
7070
+ "\">";
7171
if (stack1 = helpers.title) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
@@ -97,8 +97,8 @@ helpers = helpers || Handlebars.helpers; data = data || {};
9797
else { stack1 = depth0.body; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
9898
buffer += escapeExpression(stack1)
9999
+ "</p>\n</div>\n<div class=\"well\">\n <a href=\"/posts\" class=\"btn btn-primary\">Back to posts</a>\n <a href=\"/posts/del/";
100-
if (stack1 = helpers.id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
101-
else { stack1 = depth0.id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
100+
if (stack1 = helpers._id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
101+
else { stack1 = depth0._id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
102102
buffer += escapeExpression(stack1)
103103
+ "\" class=\"btn btn-danger del\">Delete</a>\n</div>\n";
104104
return buffer;

app/templates/posts_del_view.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p>Sure to delete?</p>
22
<div>
33
<button class="btn btn-danger del">Delete it!</button>
4-
<a href="/posts/{{id}}" class="btn">Cancel</a>
4+
<a href="/posts/{{_id}}" class="btn">Cancel</a>
55
</div>

app/templates/posts_index_view.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ul>
55
{{#each models}}
66
<li>
7-
<a href="/posts/{{id}}">{{title}}</a>
7+
<a href="/posts/{{_id}}">{{title}}</a>
88
</li>
99
{{/each}}
1010
</ul>

app/templates/posts_show_view.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
</div>
55
<div class="well">
66
<a href="/posts" class="btn btn-primary">Back to posts</a>
7-
<a href="/posts/del/{{id}}" class="btn btn-danger del">Delete</a>
7+
<a href="/posts/del/{{_id}}" class="btn btn-danger del">Delete</a>
88
</div>

app/views/posts_del_view.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ var BaseView = require('./base_view');
33
module.exports = BaseView.extend({
44
className: 'posts_del_view',
55
postInitialize: function() {
6-
this.post = this.getTemplateData();
6+
console.log(this.model, 77);
7+
console.log(this.options,88);
78
},
89
events: {
910
'click .del': 'del'
1011
},
1112
del: function() {
1213
var that, promise;
13-
promise = this.post.destroy();
14+
that = this;
15+
promise = this.model.destroy();
1416
promise
1517
.success(function() {
1618
that.app.router.redirectTo('/posts');

server/lib/data_adapter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function DataAdapter(options) {
1919
// `callback`: Callback.
2020
//
2121
DataAdapter.prototype.request = function(req, api, options, callback) {
22-
var Model;
22+
var Model, method;
2323

2424
if (arguments.length === 3) {
2525
callback = options;
@@ -29,7 +29,9 @@ DataAdapter.prototype.request = function(req, api, options, callback) {
2929
Model = getModel(api);
3030
if (!Model) return callback(new Error('Model not found'));
3131

32-
Model[api.method.toLowerCase()](req, api, function(err, body) {
32+
method = api.method.toLowerCase().replace('delete', 'del');
33+
34+
Model[method](req, api, function(err, body) {
3335
var res = {
3436
statusCode: 200 // TODO Rules to determine statusCode.
3537
};

server/models/post.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ var mongoose = require('mongoose'),
44
_ = require('underscore');
55

66
PostSchema.statics.get = function(req, api, callback) {
7-
console.log(api);
87
var id = req.param('id');
9-
console.log(req.params);
10-
console.log(id);
118
if (id) {
129
this.findById(id).exec(callback);
1310
} else {
14-
this.find().exec(callback);
11+
this
12+
.find()
13+
.where('del', false)
14+
.exec(callback);
1515
}
1616
};
1717

@@ -25,21 +25,23 @@ PostSchema.statics.post = function(req, api, callback) {
2525
};
2626

2727
PostSchema.statics.put = function(req, api, callback) {
28-
var update, data, options, post;
28+
var id, update, data, options, post;
29+
id = req.param('id');
2930

30-
if (!api.id) return callback('Cannot update without id');
31+
if (!id) return callback('Cannot update without id');
3132

32-
update = {_id: api.id};
33+
update = {_id: id};
3334
data = _.pick(req.body, ['title', 'body']);
3435
options = {};
3536

3637
this.update(update, data, options, callback);
3738
};
3839

3940
PostSchema.statics.del = function(req, api, callback) {
40-
if (!api.id) return callback('Cannot delete without id');
41+
var id = req.param('id');
42+
if (!id) return callback('Cannot delete without id');
4143

42-
this.update({_id: api.id}, {del: true}, callback);
44+
this.update({_id: id}, {del: true}, callback);
4345

4446
};
4547

server/server.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ var preRendrMiddleware = [
102102
];
103103

104104
function buildApiRoutes(app) {
105-
var fnChain = preRendrMiddleware.concat(rendrMw.apiProxy());
105+
var fnChain, api, goNext;
106+
107+
api = '/api';
108+
goNext = function(req, res, next) {
109+
next();
110+
};
111+
112+
app.get(api + '/posts/:id', goNext);
113+
app.put(api + '/posts/:id', goNext);
114+
app.del(api + '/posts/:id', goNext);
115+
116+
fnChain = preRendrMiddleware.concat(rendrMw.apiProxy());
106117
fnChain.forEach(function(fn) {
107118
app.use('/api', fn);
108119
});

0 commit comments

Comments
 (0)