Backbone collection.fetch(),解析不起作用

2024-01-03

我正在使用backbone.js,我想从服务器获取我的集合的数据:

var Account = Backbone.Model.extend();
var AccountList = Backbone.Collection.extend({
        model: Account,

        url: '/pfp/accounts/service',

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

    });
var accountList = new AccountList;
accountList.fetch();
console.log(accountList.toJSON()); 

服务器响应:

{"Data":
[{"accountid":"101752","account_name":"hijklmnopq","userid":"1","comment":"mnopqrstu","creation_date":"6 Jan 2008","account_type":"2","acc_type_name":"Дебетовая карта","currency":"144","letter_code":"","start_balance":"90.000.000","start_balance_raw":90000000.000,"to_total":true},
{"accountid":"144924","account_name":"emabcefghijklmnopqr","userid":"1","comment":"lmno","creation_date":"19 Jan 2008","account_type":"4","acc_type_name":"Банковский счёт","currency":"113","letter_code":"Le","start_balance":"360.000.000,00","start_balance_raw":360000000.000,"to_total":true},
...

accountList.toJSON() 返回空数组 ([ ])。 请帮我看看代码有什么问题。


因此,正如 WiredPrairie 所说,您需要更改这些行:

accountList.fetch();
console.log(accountList.toJSON()); 

to:

accountList.fetch({
  success: function(collection){
    // This code block will be triggered only after receiving the data.
    console.log(collection.toJSON()); 
  }
});
// JS will likely reach this line before receiving the data.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Backbone collection.fetch(),解析不起作用 的相关文章

随机推荐