【发布时间】:2014-12-16 21:00:11
【问题描述】:
我正在尝试更新表示为客户端集合中模型的数据和 mongo db 集合中的文档。事件触发执行此操作的方法是单击视图上的元素。 客户端是一个 Backbone 应用程序。
在服务器端,我使用带有 Express 的 node 和带有 Waterline ORM 的 Mongodb。对于这个请求,我使用:
app.put('/posts/:id', function(req, res){
app.models.posts.update( req.params.id, function(err, result){
if(err) return res.status(500).json({ err: err });
res.json( result );
});
});
视图中的事件方法是:
updatePost: function(e){
e.preventDefault();
//update the new content of the fields on the server.
//find model to update in the collection by colleciton.findWhere.
this.modelid = $(e.currentTarget).attr('id');
this.modeltoupdate = this.collection.findWhere( { id: this.modelid } );
//change model attributes as needed by model.set().
this.modeltoupdate.set(
{
title: $('#title').text(),
body: $('#body').text(),
});
//save the model on server with wait:true to wait server aknowledge, and add to colelction adn rerender view.
this.modeltoupdate.save(
{
wait:true,
success: function(){
this.collection.add( this.modeltoupdate );
this.render();
this.renderPost();
},
}
);
},
这个视图的模板是:
<script type="text/template" id="postTemplate">
<a href="/">All Posts</a>
<p id='author'>{{author}}</p>
<p id='title' contenteditable='false'>{{title}}</p>
<p id='createdat'>{{createdAt}}</p>
<p id='body' contenteditable='false'>{{body}}</p>
<a id='{{id}}' class='editpost' href=''>Edit this post</a>
<a id='{{id}}' class='updatepost' href=''>Update this post</a>
<a href="/">All Posts</a>
</script>
但我在 Safari 检查器的资源列表中看到一个始终加载的带有旋转图标的资源。单击它并检查与其相关的请求和响应显示请求是模型的属性,其中包含预期的更新字段,但响应显示加载 gif,没有响应。
内容可编辑属性没有问题,当单击更新此帖子链接时,它们都设置为 true。
这与我创建的服务器端路由、req 参数或 req 正文有关吗? BB 将它们发送到目标/posts/548e00e61e96a70d0fa4ad50,所以/posts/:id,看来我的 app.put() url 是正确的。
【问题讨论】:
标签: node.js backbone.js express sails.js waterline