加载Markdown时出现奇怪的Requirej行为



我有一个requirejs模块,我正试图在其中加载markdownjs。这是文件:

define(function(require) {
  'use strict';
  var Backbone = require('backbone');
  var blogCollectionTemplate = require('hbs!app.templates/blog.collection.view');
  var BlogModelView = require('views/blog.item.view');
  var markdown = require('markdown');
  var BlogCollectionView = Backbone.View.extend({
    template: blogCollectionTemplate,
    initialize: function() {
      debugger;
    },
    render: function() {
      this.$el.html(this.template());
      this.renderAll();
      return this;
    },
    renderAll: function() {
      var that = this;
      this.collection.each(function(blog) {
        that.renderItem(new BlogModelView({model: blog}));
      });
    },
    renderItem: function(blog) {
      this.$el.find('#blog-posts').append(blog.render(blog).el);
    }
  });
  return BlogCollectionView;
});

这是我的require.config:

define(function() {
  require.config({
    hbs : {
      templateExtension : 'hbs',
      disableHelpers: true,
      disableI18n : true
    },
    shim: {
      'backbone': {
        deps: [
          'underscore',
          'jquery'
        ],
        exports: 'Backbone'
      },
      bootstrap: {
        deps: [ 'jquery' ]
      },
      DlHighlight: {
        exports: 'DlHighlight'
      },
      'jqueryMockAjax': {
        exports: '$.mockjax',
        deps: ['jquery']
      },
      json2 : {
        exports: "JSON"
      },
      'underscore': {
        exports: '_'
      }
    },
    paths: {
      backbone: 'libs/backbone/backbone',
      bootstrap: 'libs/bootstrap/dist/js/bootstrap',
      DlHighlight: 'libs/hl/hl-all',
      highlight: 'libs/highlightjs/highlight.pack',
      jquery: 'libs/jquery/jquery',
      jqueryMockAjax: 'libs/jquery-mockjax/jquery.mockjax',
      markdown: 'libs/markdown/lib/markdown',
      text: 'libs/text/text',
      underscore: 'libs/underscore/underscore',
      hbs: 'libs/hbs/hbs',
      handlebars: 'libs/hbs/Handlebars',
      i18nprecompile: 'libs/hbs/hbs/i18nprecompile',
      json2 : 'libs/hbs/hbs/json2',
      'app.templates': '../templates/'
    }
  });
});

这是一个奇怪的行为。在我的initialize中,当我调用调试器时,我可以访问我导入的markdown对象,但如果我尝试使用markdown对象,那么它总是undefined。如果我将markdown放在initializerender方法之一中,则markdown变量为undefined。这毫无意义,但requirejs是否存在一些我不理解的行为。有什么想法吗?

在阅读了一个bower安装markdown js的代码后,我发现bower所安装的东西无法正常使用RequireJS。尝试添加此垫片:

"markdown": {
    exports: "markdown"
}

至于为什么您能够在没有填充程序的情况下在调试器中获得markdown的值,我相信您是从全局范围获得的(可能没有意识到)。Markdown js在加载时将自己安装到全局作用域(window.markdown,如果没有其他变量干扰,则可以作为markdown访问)中。这是猜测,但符合事实。

您可以在define子句本身中要求所有这些模块:

define([
    'backbone',
    'hbs!app.templates/blog.collection.view',
    'views/blog.item.view',
    'markdown'
], function (
    Backbone,
    blogCollectionTemplate,
    BlogModelView,
    markdown
) {
    'use strict';
    // do stuff
});

此外,你所说的"如果我在初始化或某个渲染方法中进行标记"是什么意思?你的意思是在初始化和渲染时明确要求降价吗?有没有理由不在上面标注的define子句中加载降价?

如果您在initialize或render中明确要求markdown,我不确定为什么会返回undefined,但请告诉我,将需求移动到define子句是否解决了您的问题(或者您不能做到这一点)。也许您可以将代码发布到markdown模块中(如果不是库的话)?

最新更新