首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >车把不出口吗?

车把不出口吗?
EN

Stack Overflow用户
提问于 2014-12-11 14:13:31
回答 1查看 334关注 0票数 3

问题

在执行编译后的工具栏模板时,不导出全局工具栏对象。注意:全局主干对象正在工作。

请参阅,当代码App.templates.todos试图在todos.js文件中执行时,由于未定义App.templates.todos而失败。最后,这是因为templates.js文件中的第三行无法执行,因为没有定义全局Handlebars对象。

为什么那个对象不被定义?我对require.js做了什么错事?

更新:--我已经验证了handlebars.runtime.js文件实际上在 templates.js文件之前执行,所以require.js在加载todos.js文件时以正确的顺序运行它们。

Bower组件

代码语言:javascript
复制
{
  "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"
  }
}

main.js

代码语言:javascript
复制
/*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();
});

todos.js

代码语言:javascript
复制
/*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;
});

templates.js

代码语言:javascript
复制
this["App"] = this["App"] || {};
this["App"]["templates"] = this["App"]["templates"] || {};
this["App"]["templates"]["stats"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {
EN

回答 1

Stack Overflow用户

发布于 2014-12-11 14:29:39

来自正式文件 of RequireJS:

shim只设置代码关系。要加载属于或使用shim的模块,需要正常的require/define调用。设置shim本身不会触发加载代码。

因此,首先,您需要以某种方式调用车把,然后尝试在templates.js中使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27424901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档