【问题标题】:How to remap a Durandal viewmodel如何重新映射 Durandal 视图模型
【发布时间】:2014-03-31 06:53:04
【问题描述】:

这是我在 Durandal 项目中的 main.js 文件。

我要做的是设置,以便名称“upload-item”解析为“upload-item”或“upload-item-prehtml5”,具体取决于是否定义了File

requirejs.config({
  paths: {
    'text': '../lib/require/text',
    'durandal': '../lib/durandal/js',
    'plugins': '../lib/durandal/js/plugins',
    'transitions': '../lib/durandal/js/transitions',
    'knockout': '../lib/knockout/knockout-2.3.0',
    'bootstrap': '../lib/bootstrap/js/bootstrap',
    'jquery': '../lib/jquery/jquery-1.9.1.min',
    'jquery-ui': '../lib/jquery-ui/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min',
    'moment': '../lib/moment/moment',
    'knockout-jqueryui': '../lib/knockout/knockout-jqueryui.min',
    'file-size-formatting': '../lib/wone/file-size-formatting'
  },
  shim: {
    'bootstrap': {
      deps: ['jquery'],
      exports: 'jQuery'
    }
  }
});

define(['durandal/system', 'durandal/app', 'durandal/viewLocator'], function (system, app, viewLocator) {
  //>>excludeStart("build", true);
  system.debug(true);
  //>>excludeEnd("build");

  var filetype = typeof(File);
  if (filetype == 'undefined') {
    //apply pre-html5 fixups
    require.config({
      map: {
        '*': {
          'upload-item': 'upload-item-prehtml5'
        }
      }
    });
  }

  app.title = 'Jumbo File Transfer';

  //specify which plugins to install and their configuration
  app.configurePlugins({
    router: true,
    dialog: true,
    widget: {
      kinds: ['expander']
    }
  });

  app.start().then(function () {
    //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
    //Look for partial views in a 'views' folder in the root.
    viewLocator.useConvention();

    //Show the app by setting the root view model for our application.
    app.setRoot('shell');
  });
});

在IE8上测试显示发生了对require.config的调用并添加了映射,但似乎没有达到我预期的效果:upload-item.js和upload-item.html在我预期的时候被加载了upload-item-prehtml5.js 和 upload-item-prehtml5.html 被加载。

如果这是错误的解决方法,那么执行这种条件解析的正确方法是什么?

【问题讨论】:

    标签: durandal


    【解决方案1】:

    这不是我最初想要的完全,但我发现你可以这样做:

    var prehtml5 = (typeof (File) == 'undefined');
    
    requirejs.config({
      paths: {
        ...
        'upload-item': prehtml5 ? 'upload-item-prehtml5' : 'upload-item'
      },
      shim: {
        'bootstrap': {
          deps: ['jquery'],
          exports: 'jQuery'
        }
      }
    });
    

    路径重新映射似乎扩展到文件名。通常你不会列出 main.js 的兄弟姐妹,但你可以,如果你这样做了,那么你可以重新映射它们,包括文件名。

    【讨论】:

    • 随后这已停止工作。这不是愚人节玩笑,但事实证明是这样的。
    猜你喜欢
    • 2013-04-11
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多