我尝试使用 Backbone 执行“PUT”操作。它从服务器获取记录。但是 neighter where
和 get
方法确实检索到一个解决方案(下面代码的 console.log 的结果是 []
),我可以编辑它(使用set
)并可以保存
。
解决方案可能非常简单,但仍然没有意识到问题所在!
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;
}
关于javascript - Backbone 中获取请求时未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588198/