【问题标题】:Can't figure out how to wrap function in Ember run loop无法弄清楚如何在 Ember 运行循环中包装函数
【发布时间】:2015-05-21 13:21:47
【问题描述】:

我有一个集成了 2 个第三方库、imagesLoaded 和 Isotope 的组件。

在浏览器和 cli 模式下运行测试时出现冲突的测试失败。错误是:

Error: Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run

TypeError: 'undefined' is not an object (evaluating 'self.$().isotope')

当我尝试在 ember 运行循环中包装回调时,它们会在 cli 模式下传递,但在浏览器模式下会失败。我似乎找不到合适的组合。这个问题似乎发生在imagesLoaded的回调中,好像我删除了那个插件,它似乎通过了。

我尝试了多种组合,但这是我最新的代码。如果有人对如何正确使用此组件中的运行循环有所了解,那将很有帮助。

handleLoad: function() {
  let self = this;

  Ember.run.scheduleOnce('afterRender', this, function(){      
    self.$().imagesLoaded( function() {

        Ember.run(function() {
          self.$().isotope({itemSelector: ".card-container"});
          self.$().isotope('shuffle');
        });

    }); // imagesLoaded
  }); // Ember.run

}.on('didInsertElement')

【问题讨论】:

    标签: ember.js runloop imagesloaded


    【解决方案1】:

    您必须将现有代码包装在一个

    Ember.run(function () {
    //  Ember.run.scheduleOnce('afterRender....
    });
    

    因为这告诉 Ember 运行循环在哪里开始和结束 - 在生产/开发中,这是由 Ember 自己为您完成的,但在测试中您必须包装它。

    作为替代方案,您可以通过显式调用手动启动和结束运行循环(参见API Docs):

    Ember.run.begin() // <-- tell Ember that your starting a run loop
    
    //Ember.run.scheduleOnce('afterRender', ...
    //more ember run loops here
    
    Ember.run.end(); // <-- tell Ember that the run loop ends here
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2023-03-25
      • 1970-01-01
      • 2013-04-03
      • 2017-09-11
      相关资源
      最近更新 更多