我有一个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
在initialize
或的一个render
方法,该markdown
变量undefined
。这没有任何意义,但是有一些我不了解的行为requirejs
。有任何想法吗?
阅读了markdown-js的Bower安装代码之后,我发现Bower安装的内容不能按原样与RequireJS一起使用。尝试添加此垫片:
"markdown": {
exports: "markdown"
}
至于为什么您markdown
无需调试程序就能在调试器中获得价值,我相信您是从全局范围中获得的(也许没有意识到)。Markdown-js在加载时将自身安装到全局范围内(window.markdown
然后可以访问该域,就像markdown
没有其他变量对其进行访问一样)。这是推测,但符合事实。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句