【问题标题】:Using AMD module as an Aurelia ViewModel使用 AMD 模块作为 Aurelia ViewModel
【发布时间】:2015-04-04 19:24:28
【问题描述】:

我和 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,事实上,在 Durandal 应用程序中使用了 viewModel。

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

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

谢谢。

【问题讨论】:

    标签: javascript requirejs durandal amd aurelia


    【解决方案1】:

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

    1. 在项目根目录中创建一个文件夹amd
    2. 将原始 Durandal VM(来自您的示例)原样复制到其中。
    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 LifeCycle 等,但我想这是一个好的开始

    【讨论】:

    • 嗨@zewa666 如果可行,那么在 aurelia 上使用 starndard amd 确实是一个很好的解决方法,这是一个很好的转换,而且不费吹灰之力,很高兴听到项目越来越大。期待 Aurelia 的发布版本。
    • 我创建了一个 repo github.com/zewa666/durelia,它应该可以帮助您完成第一步。让我们看看我们能走多远
    【解决方案2】:

    这是与 Aurelia 一起使用的 example AMD module

    define(["require", "exports"], function (require, exports) {
      var Welcome = (function () {
        function Welcome() {
          this.heading = "Welcome to the Aurelia Navigation App (VS/TS)!";
          this.firstName = "John";
          this.lastName = "Doe";
        }
        Object.defineProperty(Welcome.prototype, "fullName", {
          get: function () {
            return this.firstName + " " + this.lastName;
          },
          enumerable: true,
          configurable: true
        });
        Welcome.prototype.welcome = function () {
          alert("Welcome, " + this.fullName + "!");
        };
        return Welcome;
      })();
      exports.Welcome = Welcome;
    });
    

    它实际上是由 TypeScript source file 生成的

    export class Welcome {
      public heading: string;
      public firstName: string;
      public lastName: string;
    
      constructor() {
        this.heading = "Welcome to the Aurelia Navigation App (VS/TS)!";
        this.firstName = "John";
        this.lastName = "Doe";
      }
    
      get fullName() {
        return this.firstName + " " + this.lastName;
      }
    
      welcome() {
        alert("Welcome, " + this.fullName + "!");
      }
    }
    

    您可以在 GitHub 存储库中关注 the sample's instructions 以使用该示例。

    【讨论】:

    • 感谢@MikeGraham,这似乎是一个不错的解决方案,但转换起来有点复杂,因为我看到文档解释说 Aurelia 可以与常规 AMD 模块一起使用。但至少是一种可能的方法。非常感谢您的意见。很棒的框架
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多