使用 dustmotes-if 助手

Using the dustmotes-if helper

我正在通过以下方式加载(通过 requirejs)(最新版本的)dust 和 dust-helpers

define.amd.dust = true;
require(["./Scripts/dust-full"], function (dust) {
    require(["./Scripts/dust-helpers"], function () {
        ... //my other dust custom helpers
    });
});

一切正常 - 我能够利用 dust/dust-helper 逻辑。但现在我想利用 dust-motes if helper 并尝试了以下

define.amd.dust = true;
require(["./Scripts/dust-full"], function (dust) {
    require(["./Scripts/dust-helpers"], function () {
        require(["./Scripts/dustmotes-if"], function () {
            ... //my other dust custom helpers
        });
    });
});

我也尝试删除 "require(["./Scripts/dustmotes-if"], function () {" 逻辑并简单地替换为 "require(["./Scripts/dustmotes-if"]) ;" - 虽然代码执行了,'if' 助手 没有 加载到助手集合中,因此在 dust 代码中引用 'if' 助手导致 "Helper 'if' does not exist".

有没有人试过这个或者有什么想法。 http://www.dustjs.com/ 中的参考并没有真正涵盖上述情况 - 而且,许多 documentation/forum-posts 在线参考旧版本的 dustjs 和 requirejs

正如@Interrobang 所建议的那样,问题是它应该作为 IIFE 加载。

注意事项

  • 因为 IIFE 在它自己的范围内运行并且需要灰尘 作为参数传递的对象,我将灰尘对象分配给 window 对象(见下文)
  • 尘埃 - 如果引用了一个旧的 dust 库的版本,因此必须对其进行更改 - 从 “...要求('dustjs-linkedin')...”到“...要求('dust-full')...”...

所以最终的代码如下

...
define.amd.dust = true;
require(["./Scripts/dust-full"], function (dust) {
    require(["./Scripts/dust-helpers"], function () {
        window.dust=dust;
        $.getScript("./Scripts/dustmotes-if.js")
        .done(function(script,textStatus) {
            console.log(textStatus);
        })
        .fail(function(jqxhr,settings,exception) {
            console.log('Triggered ajaxError handler:'+exception.message);
        });
        ...

在尘埃中-if.js

...
})(typeof exports !== 'undefined' ? module.exports = require('dust-full') : dust);