javascript - 使用 AMD 模块作为 Aurelia ViewModel

标签 javascript requirejs durandal amd aurelia

我在玩 Aurelia,看起来很不错,我在一些项目中使用 Durandal,这绝对适合我的需求。

使用 EC6 中的新类定义非常棒。但现在我正在准备一些东西,其中我需要使用带有 requireJs 的经典 AMD 模块,如下所示:

define("testModule",
[],
function() {
    "use strict";
    console.log('testModule loaded');

    var testModule = function() {
        var that = this;

        this.variable = 10;

        that.getVariable = function(){
            alert('function executed ' + that.variable);
        };
    }

    return testModule;
});

根据 Aurelia 的文档,我发现可以使用 testModule 之类的东西作为 ViewModel,事实上 viewModel 已在 Durandal 应用程序中使用。

但经过一番尝试后,我无法使其正常工作。

有人遵循什么想法或方法来做到这一点? 最重要的是,这可能吗?我认为只是为了确认兼容。

谢谢。

最佳答案

我们已经对此进行了一些尝试。这就是我们的想法。该作品基于 Skeleton Navigation App:

  1. 在项目根目录中创建一个文件夹 amd
  2. 将您的原始 Durandal 虚拟机(来自您的示例)按原样复制到其中。
  3. src 内创建一个 Wrapper VM,也命名为 testmodule.js,内容如下:

    export class TestModule {
      constructor() {
      }
    
      activate() {
        return System.import('../amd/testmodule').then( (result) => {
          if(typeof result === 'function')
            Object.assign(this, new result());
          else
            Object.assign(this, result);
        });
      }
    }
    
  4. 享受吧:)

这本质上是包装您的原始 DurandalVM 并将其公开为新的 AureliaVM。这只是一个开始,肯定有一些事情需要考虑,比如尊重 Durandals 生命周期等。但我想这是一个好的开始

关于javascript - 使用 AMD 模块作为 Aurelia ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28335502/

相关文章:

javascript - JQuery onclick slideDown 元素和更改 id 只工作一次

requirejs - system.js 可以代替 require.js

session - 如何在 SPA 应用程序(客户端)中检测 Windows ACS session 是否过期

javascript - VS 2013 中针对 javascript AMD 模块的 Intellisense

knockout.js - 使用 childRouter 时的 Durandal 路由器/生命周期事件

javascript - 我如何通过 javascript(d3) 更改 Node CSS 的样式

javascript - 网格的过滤器存储在两个不同的列上

javascript - html/js : how to get username of local system in html or in js?

javascript - 错误 : ReferenceError: document is not defined

javascript - 无法通过 gulp 在我的 karma runner Mocha 测试中使用 require