【问题标题】:Durandal requiring viewmodel from shellDurandal 需要来自 shell 的视图模型
【发布时间】:2015-08-29 08:47:51
【问题描述】:

在我的应用中,我有一个全球玩家。在 shell 模块中,我需要播放器的视图模型,因为我想知道播放器是否正在播放,如果是,我将一个类添加到应用程序的容器(在 shell 中)。

问题是我还需要来自播放器 VM 的 shell,因为我在 shell 中的应用程序中使用了一些功能。

但是当从外壳请求播放器模块时,从播放器请求外壳返回undefined。如果我不需要播放器,则shell正常通过。

shell.js

define(['viewmodels/player'], function(player) {
 return {
  player: player
 }
})

player.js

define(['viewmodels/shell'], function(shell) {
 console.log('shell:', shell) // undefined
})

我不知道发生了什么。

【问题讨论】:

  • 这确实会产生循环依赖,并打破 require!请参阅下面的答案!

标签: javascript durandal durandal-2.0


【解决方案1】:

嗯! 我想我曾经遇到过这个问题。他们发生的方式是要求检查shell需要什么。然后它会看到播放器模块并修复它,在里面你需要 shell。循环参考。不过我们确实解决了。

我们确实这样做了,我正在编写伪代码,但你应该可以试试这个。

有很多方法,这是一种简单的方法。打开你的播放器并这样做。

 var shell = require('shell'); //using this style to work around circular reference

【讨论】:

    【解决方案2】:

    我将尝试帮助一些实现替代方案!

    1º!播放器可以是服务(单例),并且在两种视图模式中都需要,

    // app/services/player.js
    require([], function(){/*Player*/});
    
    // shell
    require(['services/player'], function(player){/* SHEll */});
    
    // ohter view
    require(['services/player'], function(player){/* other view*/});
    

    2º 您可以使用发布/订阅模式! Durandal 对此表示支持!

    // Alert that play has been clicked
    app.trigger('player:play');
    
    // subscribe play
    app.on('player:play', doSomething);
    
    // deactivate subscription
    app.off('player:play', doSomething);
    

    查看documentation!

    Booth 工作正常且耦合度低...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多