mardi 4 octobre 2016

Ember open new window avoiding pop-up blocker in a successful promise

I am struggling to find a way to open a new window inside a then() promise after creating a store record and persisting.

The following code is blocked by chrome's pop-up blocker

var obj = this.get('store').createRecord('user', {
  first_name: 'John',
  last_name: 'Smith'
});

obj.save()
  .then(function (user) {
    window.open('http://google.com', '_blank'); // this is blocked by chrome pop-up blocker
  })
  .catch(function (error) {
    this.set('errors', error.errorArray); // display errors in the template
  });

This code works and bypasses the pop-up blocker but my problem is that if the backend throws errors during save, I don't want to open the new window since in the end the user was not successfully created and I want them to still be on the main window where they can see the error. I can close the window if I get errors but that does not feel right since it will look like a flicker of opening and closing the window immediately.

var window_handle = window.open('', '_blank');

var obj = this.get('store').createRecord('user', {
  first_name: 'John',
  last_name: 'Smith'
});

obj.save()
  .then(function (user) {
    window_handle.location.href = 'http://google.com'; // this is NOT blocked by chrome pop-up blocker
  })
  .catch(function (error) {
    this.set('errors', error.errorArray); // display errors in the template
  });

Does anyone have a better idea?




Aucun commentaire:

Enregistrer un commentaire