骨干不做与保存后的保存()PUT请求 [英] Backbone not making a put request with save() after save

查看:239
本文介绍了骨干不做与保存后的保存()PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了骨干一个非常有趣的问题,我有这样的功能在我的观点之一:

  addpeople:功能(){
        变种authArray = _.clone(this.model.get(authorizedUsers));
        变种值= $(附加输入。)VAL()分割(,)。;
        values​​.forEach(函数(值){
            authArray.push(值);
        });
        this.model.set(authorizedUsers,authArray);
        this.model.save();    },

这个函数是单击按钮时调用。该版本功能触发变更事件,因为我克隆我的数组,但由于某些原因 this.model.save()不会被调用,又名服务器永远不会收到一个PUT请求。当我刷新页面我回去模型的老态。

但是,如果我不克隆阵列和更改的功能,这样的:

  addpeople:功能(){
        变种authArray = this.model.get(authorizedUsers);
        变种值= $(附加输入。)VAL()分割(,)。;
        values​​.forEach(函数(值){
            authArray.push(值);
        });
        this.model.set(authorizedUsers,authArray);
        this.model.save();    },

这一次PUT请求发送成功,但是网页无法重新呈现,因为不会触发更改事件。当我刷新页面,我可以看到更新后的模型。

我知道我可以手动在第二个例子中触发更改事件,但我更好奇,为什么我的 this.model.save()不是在叫第一个例子..

要帮助您了解更多的问题,我的模型看起来是这样的:

  VAR PostModel = Backbone.Model.extend({
    urlRoot:'/鸣叫,
    idAttribute:'_id',
    默认值:{
        名称: '',
        注释: [],
        标签:[],
        authorizedUsers:[],
        postedBy:'',
        创建日期: ''
    },
});

和我的node.js更新功能如下:

  exports.updateTweet =功能(REQ,RES){
  的console.log(入门叫!)
  VAR更新= req.body;
  变种的id = req.url.split(/)[2];  Post.update({_id:ID},{$集:{authorizedUsers:req.body.authorizedUsers}},功能(ERR,POST){
      如果(ERR)返回的HandleError(ERR);
  });
  重发();};


解决方案

之所以更改为您的第二个例子没有触发是因为它是同一个对象, 骨干忽略它。因此,没有更改事件触发。

至于为什么第一次失败了;你有没有验证你的模型?可以是某种验证了也许空字符串? VAL()可以返回一个空字符串和拆分()的空字符串将返回 [ ]

此外,您的默认值应该是一个功能,否则,所有的模型将有意见相同的实例标记 authorizedUsers

从主干文档。


  

请记住,在JavaScript中,对象是按引用传递的,所以如果你有一个对象作为默认值,它会被所有实例之间共享。取而代之的是,默认为函数。


数组是对象了。

  VAR PostModel = Backbone.Model.extend({
    urlRoot:'/鸣叫,
    idAttribute:'_id',
    默认设置:功能(){
      返回{
          名称: '',
          注释: [],
          标签:[],
          authorizedUsers:[],
          postedBy:'',
          创建日期: ''
      }
  }
});

最后, array.forEach()不适用于IE8及以上。

I am experiencing a really interesting problem with backbone, I have a function like this in one of my views:

addpeople :function(){
        var authArray = _.clone(this.model.get("authorizedUsers"));
        var values = $(".add-input").val().split(",");
        values.forEach(function(value) {
            authArray.push(value);
        });
        this.model.set("authorizedUsers" , authArray);
        this.model.save();

    },

this function gets called when a button is clicked. This version of the function triggers a change event because I am cloning my array, but for some reason this.model.save()never gets called, aka the server never receives a PUT request. When I refresh the page I go back to the old state of the model..

However if I dont clone the array and change the function to, this:

addpeople :function(){
        var authArray = this.model.get("authorizedUsers");
        var values = $(".add-input").val().split(",");
        values.forEach(function(value) {
            authArray.push(value);
        });
        this.model.set("authorizedUsers" , authArray);
        this.model.save();

    },

This time the PUT request is sent successfully, but the page is not re-rendered because a change event is not triggered. When I refresh the page I can see the updated model..

I know that I can manually trigger a change event in the second example but I am more curious about why my this.model.save() is not called in the first example..

To help you understand the problem more my model looks something like:

var PostModel = Backbone.Model.extend({
    urlRoot : '/tweet',
    idAttribute: '_id',
    defaults:{
        name: '',
        comments: [],
        tags: [],
        authorizedUsers: [],
        postedBy : '',
        dateCreated: ''
    },


});

and my node.js update function looks like:

exports.updateTweet =  function(req,res){
  console.log("Getting called ! ")
  var update = req.body;
  var id = req.url.split("/")[2];

  Post.update({ _id: id }, { $set: { authorizedUsers: req.body.authorizedUsers }}, function (err, post) {
      if (err) return handleError(err);
  });
  res.end();

};

解决方案

The reason why change didn't trigger for your second example is because it is the same object and Backbone ignore it. Hence no change event triggered.

As for why the first one failed; do you have validator for your model? May be something that validating for empty string perhaps? val() can return an empty string and split() on empty string will return [""]

Also, your defaults should be a function otherwise all your model would have the same instance of comments, tags and authorizedUsers

From Backbone doc.

Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.

Arrays are object too.

var PostModel = Backbone.Model.extend({
    urlRoot : '/tweet',
    idAttribute: '_id',
    defaults: function(){
      return {
          name: '',
          comments: [],
          tags: [],
          authorizedUsers: [],
          postedBy : '',
          dateCreated: ''
      }
  }
});

Lastly, array.forEach() is not available on IE8 and older.

这篇关于骨干不做与保存后的保存()PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆