javascript - Backbone collection.fetch(), parse doesn't work - Stack Overflow

I'm using backbone.js and I want to fetch data for my Collection from server:var Account = Backbon

I'm using backbone.js and I want to fetch data for my Collection from server:

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()); 

Server response:

{"Data":
[{"accountid":"101752","account_name":"hijklmnopq","userid":"1","ment":"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","ment":"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() return empty array ([ ]). Please help me what is wrong with the code.

I'm using backbone.js and I want to fetch data for my Collection from server:

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()); 

Server response:

{"Data":
[{"accountid":"101752","account_name":"hijklmnopq","userid":"1","ment":"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","ment":"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() return empty array ([ ]). Please help me what is wrong with the code.

Share Improve this question asked Mar 13, 2013 at 11:38 MargaritaMargarita 781 silver badge7 bronze badges 3
  • 2 This is a mon question -- fetch is asynchronous as documented. You'll need to add a success callback in the call to fetch, or connect to one of the reset/add events of the collection. – WiredPrairie Commented Mar 13, 2013 at 12:04
  • stackoverflow./questions/7259712/… – WiredPrairie Commented Mar 13, 2013 at 12:06
  • Also note that is should be var accountList = new AccountList(); – sQVe Commented Mar 13, 2013 at 12:15
Add a ment  | 

1 Answer 1

Reset to default 4

So, as WiredPrairie said, you need to change those lines:

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745189065a4615776.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信