在使用主干访问 api 时,我发现我只需要在响应中包含一些数据。除了关于我不需要的对象的数据之外,网络服务器还给我返回元数据。
以下解决方案有效,但感觉不对。有这样做的标准方法吗?
var accountsCollection = new AccountsCollection();
accountsCollection.fetch({success : function(collection){
var results = new AccountsCollection();
collection.each(function(item){
results.add(new AccountModel({
id: item.toJSON().result[0].id,
messageText: item.toJSON().messageText,
address1: item.toJSON().result[0].address1,
address2: item.toJSON().result[0].address2
}));
});
onDataHandler(results);
}});
编辑:这是我基于接受的答案的最终解决方案:
parse: function(response) {
var accounts = [];
_.each(response['result'], function (account) {
accounts.push(account);
});
return accounts;
}