Ember打开新窗口,避免弹出窗口拦截器成功承诺

时间:2016-10-05 01:01:21

标签: javascript ajax google-chrome ember.js popup-blocker

在创建商店记录并坚持使用后,我很难找到在then()承诺内打开新窗口的方法。

以下代码被chrome的弹出窗口阻止程序阻止

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
  });

此代码可以工作并绕过弹出窗口阻止程序,但我的问题是如果后端在保存期间抛出错误,我不想打开新窗口,因为最终用户没有成功创建,我想要它们仍然在主窗口,他们可以看到错误。如果我收到错误,我可以关闭窗口,但感觉不对,因为它看起来像是一个立即打开和关闭窗口的闪烁。

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
  });

有没有人有更好的主意?

1 个答案:

答案 0 :(得分: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
  });
相关问题