如何使用骨干网中的baucisfetch将回调值存储到全局变量?

时间:2013-12-16 08:51:21

标签: javascript backbone.js

下面是我的代码,用于计算集合并将其存储在全局变量计数器中。问题是它返回undefined。

_getCount : function(role){
            var Users = new collections.Users();
            var counter = 0;
            var self = this;

            Users.baucis(
            {
              conditions: { role : role },
              count: true
            }
          ).then(function (  ) {
                    counter = count;
                    //console.log(counter);
                });
          console.log(counter);
          return counter;

        },

1 个答案:

答案 0 :(得分:1)

count上的counter = count;变量无处声明。

你应该使用类似的东西:

 _getCount : function(role){
        var Users = new collections.Users();
        var counter = 0;
        var self = this;

        Users.baucis(
             {
               conditions: { role : role },
               count: true
             }
        ).then(function (  ) {
             counter = Users.length;
             //console.log(counter);
        });
        console.log(counter);
        return counter;

    },

Users.length正在收集underscore method