在 Backbone 中获取请求时未定义

Undefined upon get Request in Backbone

我尝试使用 Backbone 执行操作 "PUT"。它从服务器获取记录。但是更晚的 whereget 方法确实检索了一个解决方案(下面代码的 console.log 结果是 [] )我可以编辑(使用 set)和可以 save.

解决方案可以很简单,但仍然没有意识到哪里出了问题!

var Model = Backbone.Model.extend({
    idAttribute : "ID"
});
var TodosCollection = Backbone.Collection.extend({
        model : Model,
        url : myUrl
    });
var todos = new TodosCollection();
todos.fetch();

var todo = todos.where({
        ID : 2
    });
console.log(todo);
todo.set('Name', 'ChangedName');
todo.save();

您需要 Parse the response 并指定模型所在的位置。

如果有人偶然发现了这一点,可以更好地参考。 这可能看起来像

{ 
  "location": "UK",
  "users": [
      {id: "etc", name: "etc"},
      {id: "etc", name: "etc"},
      {id: "etc", name: "etc"}
  ]
  "someothermeta": "score",
  "foo": "bar"
}

您的解析函数将是:

parse: function(response) {
    return response.users;
 }