【问题标题】:Ember open new window avoiding pop-up blocker in a successful promiseEmber 打开新窗口在成功的承诺中避免弹出窗口阻止程序
【发布时间】:2017-02-13 07:19:39
【问题描述】:

在创建商店记录并保留后,我正在努力寻找一种方法来在 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
  });

有人有更好的主意吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多