获取后无法访问值

时间:2014-09-02 07:14:19

标签: javascript backbone.js

在SugarCRM 7.2.2中进行验证,我已经使用以下代码来获取Team和那些用户,但行var myTeamusers = userTeam2.getRelatedCollection('users');返回null。奇怪的是,同样的代码正在Chrome控制台上运行。

var userTeam2 = App.data.createBean('Teams',{id:'e8566565-0d27-2f09-edf5-  52c67c376d41'});//WORKING FINE
userTeam2.fetch();//WORKING FINE
var myTeamusers = userTeam2.getRelatedCollection('users');//WORKING FINE
myTeamusers.fetch({relate:true});//WORKING FINE
alert("two"+ myTeamusers); // HERE RETURING NO VALUE I.E. LENGTH OF TEAM IS 0 HERE BUT WHEN RUN ON CHROME CONSOLE SHOW IT 4 USERS IN TEAM
myTeamusers.toJSON();
alert("three"+ myTeamusers.toJSON());
alert("four"+ myTeamusers.toJSON().length);//SHOW IT 0

请告诉我上面的代码有什么问题,我在custom / modules / Cases / clients / base / views / record / record.js上使用了上面的代码

1 个答案:

答案 0 :(得分:0)

您应该在获取完成后访问userTeam2。使用success回调:

userTeam2.fetch({
    success: function () {
        userTeam2.getRelatedCollection('users');
    }
});

http://backbonejs.org/#Model-fetch