骨干collection.fetch(),解析不起作用 [英] Backbone collection.fetch(), parse doesn't work

查看:128
本文介绍了骨干collection.fetch(),解析不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Backbone.js的,我想从我的服务器收集获取数据:

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

服务器响应:

{"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()返回空数组([])。
请帮我什么是错的code。

accountList.toJSON() return empty array ([ ]). Please help me what is wrong with the code.

推荐答案

所以,WiredPrairie说,你需要改变这些行:

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

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

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.

这篇关于骨干collection.fetch(),解析不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆