【问题标题】:Updating a Backbone model persistently by Express通过 Express 持续更新 Backbone 模型
【发布时间】: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


    【解决方案1】:

    问题在于 app.put() 函数中的服务器端代码。代码片段调用这个函数时缺少一个强制参数,第二个参数没有提供给它。第二个参数是要放入现有模型的新值,由搜索条件选择,第一个参数。

    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 );
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2015-04-26
      • 1970-01-01
      • 2013-08-23
      • 2011-05-05
      相关资源
      最近更新 更多