Javascript 车把不是';不出口吗? 问题
执行编译的handlebars模板时,不会导出全局handlebars对象注意:全局主干对象正在工作 请参阅,当代码Javascript 车把不是';不出口吗? 问题,javascript,templates,backbone.js,requirejs,handlebars.js,Javascript,Templates,Backbone.js,Requirejs,Handlebars.js,执行编译的handlebars模板时,不会导出全局handlebars对象注意:全局主干对象正在工作 请参阅,当代码App.templates.todos尝试在todos.js文件中执行时,它会失败,因为未定义App.templates.todos。最终这是因为templates.js文件中的第三行无法执行,因为没有定义全局handlebar对象 为什么不能定义那个对象?我在这里对require.js做了什么错误 更新:我已经验证了handlebar.runtime.js文件实际上在templa
App.templates.todos
尝试在todos.js
文件中执行时,它会失败,因为未定义App.templates.todos
。最终这是因为templates.js
文件中的第三行无法执行,因为没有定义全局handlebar
对象
为什么不能定义那个对象?我在这里对require.js做了什么错误
更新:我已经验证了handlebar.runtime.js
文件实际上在templates.js
文件之前执行,因此require.js
在加载todos.js
文件时以正确的顺序运行它们
鲍尔元件
main.js
todos.js
templates.js
来自RequireJS的:
垫片配置仅设置代码关系。要加载
属于或使用垫片配置,则需要正常的require/define调用
需要。设置垫片本身不会触发代码加载
因此,首先您需要以某种方式调用Handlebar,然后尝试在templates.js中使用它。所以我不确定您的确切意思,因为
todos.js
顶部的require
实际上在templates.js
文件之前执行handlebar.runtime.js
文件。您不知道其中的顺序require将获取模块。你能尝试用定义来包装templates.js吗?deps
配置要求在模板
之前执行把手
,对吗?(我的意思是我最初的评论是一个问题)理论上应该是这样的,但它不可能是全局的。所以我尝试将它加载到一个define
,比如define('templates',['handlebar',function(){…}
,但它没有改变任何东西。
{
"name": "todomvc-backbone-requirejs",
"version": "0.0.0",
"dependencies": {
"backbone": "~1.1.0",
"underscore": "~1.5.0",
"jquery": "~2.0.0",
"todomvc-common": "~0.3.0",
"backbone.localStorage": "~1.1.0",
"requirejs": "~2.1.5",
"requirejs-text": "~2.0.5",
"handlebars": "~2.0.0"
},
"resolutions": {
"backbone": "~1.1.0"
}
}
/*global require*/
'use strict';
// Require.js allows us to configure shortcut alias
require.config({
// The shim config allows us to configure dependencies for
// scripts that do not call define() to register a module
shim: {
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
backboneLocalstorage: {
deps: ['backbone'],
exports: 'Store'
},
handlebars: {
exports: 'Handlebars'
},
templates: {
deps: ['handlebars'],
exports: 'App'
},
underscore: {
exports: '_'
}
},
paths: {
jquery: '../bower_components/jquery/jquery',
underscore: '../bower_components/underscore/underscore',
backbone: '../bower_components/backbone/backbone',
backboneLocalstorage: '../bower_components/backbone.localStorage/backbone.localStorage',
handlebars: '../bower_components/handlebars/handlebars.runtime',
templates: '../../../templates',
text: '../bower_components/requirejs-text/text'
}
});
require([
'backbone',
'views/app',
'routers/router'
], function (Backbone, AppView, Workspace) {
/*jshint nonew:false*/
// Initialize routing and start Backbone.history()
new Workspace();
Backbone.history.start();
// Initialize the application view
new AppView();
});
/*global define*/
define([
'jquery',
'backbone',
'handlebars',
'templates',
'common'
], function ($, Backbone, Handlebars, Templates, Common) {
'use strict';
var TodoView = Backbone.View.extend({
tagName: 'li',
template: App.templates.todos,
...
});
return TodoView;
});
this["App"] = this["App"] || {};
this["App"]["templates"] = this["App"]["templates"] || {};
this["App"]["templates"]["stats"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {