简体   繁体   中英

How to store a callback value to a global variable using baucisfetch in backbone?

Below is my code to count the collection and store it in a global variable counter. The problem is that it returns 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;

        },

Your count variable on the line counter = count; is declared nowhere.

You should go with something like :

 _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 being underscore method on collections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM