通过Express持久更新Backbone模型

我正在尝试更新在客户端的一个集合中的模型和mongo db集合中的一个文档中的数据。 该事件触发的方法,这是点击一个视图上的元素。 客户端是一个骨干的应用程序。

在服务器端,我使用带有Waterline ORM的Express和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,没有响应。

内容可编辑属性没有问题,当单击“更新此post”链接时,它们都设置为true。

这是与我创build的服务器端路线,请求params或req正文? BB把它们发送到目标/posts/548e00e61e96a70d0fa4ad50 ,所以/posts/:id ,这似乎我的app.put()url是正确的。

问题是在app.put()函数中的服务器端代码。 代码片段正在调用这个函数,缺less必要的参数,第二个参数不提供给它。 第二个参数是将被放入现有模型中的新值,它是由search条件(第一个参数)select的。

 app.put('/posts/:id', function(req, res){ app.models.posts.update( {id: req.params.id}, req.body, function(err, result){ if(err) return res.status(500).json({ err: err }); res.json( result ); }); }); 

或者只更新更改的值:

 app.put('/posts/:id', function(req, res){ var changedmodel = { id: req.params.id, title: req.body.title, body: req.body.body }; app.models.posts.update( {id: req.params.id}, changedmodel, function(err, result){ if(err) return res.status(500).json({ err: err }); res.json( result ); }); });