简体   繁体   English

加载Markdown时奇怪的Requirejs行为

[英]Strange Requirejs behavior when loading Markdown

I have a requirejs module in which I am trying to load markdownjs . 我有一个requirejs模块,我正在其中尝试加载markdownjs Here is the file: 这是文件:

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;
});

Here is my require.config : 这是我的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/'
    }
  });
});

Here is the strange behavior. 这是奇怪的行为。 In my initialize , when I hit the debugger, I have access to the markdown object that I have imported, BUT if I have try to use the markdown object, then it is always undefined . 在我的initialize ,当我点击调试器时,我可以访问导入的markdown对象,但是如果尝试使用markdown对象,则该对象始终是undefined If I put markdown in the initialize or in one of the render methods, the markdown variable is undefined . 如果将markdown放在initialize或任一render方法中,则markdown变量是undefined It makes no sense, but is there some behavior that I dont understand about requirejs . 这没有任何意义,但是关于requirejs我有一些我不了解的行为。 Any ideas? 有任何想法吗?

After reading the code of a bower installation of markdown-js, I found that what bower installs won't work with RequireJS as-is. 阅读了markdown-js的Bower安装代码后,我发现Bower安装的内容不能按原样与RequireJS一起使用。 Try adding this shim: 尝试添加此垫片:

"markdown": {
    exports: "markdown"
}

As to why were you able to get a value for markdown in the debugger without the shim, I believe you were getting it (perhaps without realizing it) from the global scope. 至于为什么不用shim就能在调试器中获得markdown的价值,我相信您是从全局范围获得的(也许没有意识到)。 Markdown-js installs itself into the global scope ( window.markdown , which is then accessible as markdown if no other variable interferes with it) when it is loaded. Markdown-js在加载时将自身安装到全局范围( window.markdown ,如果没有其他变量干扰它,则可以作为markdown访问)。 This is speculation but it fits the facts. 这是推测,但符合事实。

You can require all of those modules in the define clause itself: 您可以在define子句本身中要求所有这些模块:

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

    // do stuff
});

Also, what do you mean by "If I put markdown in the initialize or in one of the render methods"? 另外,“如果我将markdown放在初始化方法或渲染方法之一中”是什么意思? Do you mean actually explicitly requiring markdown in initialize and render? 您是说实际上在初始化和渲染中明确要求markdown吗? Is there any reason to not just load markdown in the define clause as labeled above? 有什么理由不只是在上面标记的define子句中加载markdown吗?

If you are explicitly requiring markdown in initialize or render, I am not sure why that would return undefined , but let me know if moving requirements to the define clause fixes your issue (or if you can't do that). 如果您明确要求在初始化或渲染中使用markdown,我不确定为什么它会返回undefined ,但请告知将需求移到define子句是否可以解决您的问题(或者您不能这样做)。 Perhaps you could post the code in the markdown module (if it's not a library that is)? 也许您可以将代码发布在markdown模块中(如果不是库)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM