0

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?

4

1 回答 1

0

save返回Promise。它具有成功和错误功能。您可以尝试错误功能而不是catch。 http://emberjs.com/api/data/classes/DS.Model.html#method_save

obj.save()
  .then(function (user) {
    var window_handle = window.open('', '_blank'); //if you need window_handle outside then you keep reference outside.
    window_handle.location.href = 'http://google.com'; // this is NOT blocked by chrome pop-up blocker
  },function (error) {
    this.set('errors', error.errorArray); // display errors in the template
  })
  .catch(function (error) {
    this.set('errors', error.errorArray); // display errors in the template
  });
于 2016-10-05T02:02:23.347 回答