Brent
Brent

Reputation: 2485

Jquery Deffered Two Arrays Backbone

Morning,

I seem to be hitting a wall, the issue is I want a popup to fire once my page is finished with a GET and POST request as the data in the popup needs to be correct.

Anyway I have tried to use a differed object with two pushed items but this still does not work, the popup gets fired after a few seconds. In the network tab there are still lots of request still firing. Any help would be appicated.

        var promises = [];

        _.each(models, _.bind(function (item) {
            var filter = this.resources.get(item.id);
            promises.push(filter.fetch(
                    {
                        success: function (model, response) {
                            var user = new UserModel();
                            promises.push(user.save());
                        }
                    }));
        }, this));

        $.when.apply($, promises1).then(_.bind(function () {
            var popupForm = new PopUpView();
            this.$el.append(popupForm.$el);
        }, this));

Upvotes: 0

Views: 51

Answers (1)

T J
T J

Reputation: 43166

You seems to have a typo: instead of promises, you're passing promises1 to when().

Side note: _.each() accepts the context as a third argument, no need use _.bind() explicitly.

var promises = [];

_.each(models, function(item) {
  var filter = this.resources.get(item.id);
  promises.push(filter.fetch({
    success: function(model, response) {
      var user = new UserModel();
      promises.push(user.save());
    }
  }));
}, this);

$.when.apply($, promises).then(_.bind(function() {
  var popupForm = new PopUpView();
  this.$el.append(popupForm.$el);
}, this));

Upvotes: 1

Related Questions