2

我收到以下代码的模型未定义错误。我添加了下划线、jquery、backbone 等,但我只能使用 console.log 函数返回对象,仅适用于 jquery$

是否有其他对象没有在定义函数中返回的原因?

下面是我来自 main.js 的代码:

require.config({
  paths: {
    jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min',
    underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
    backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min',
    bootstrap: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min',
    qunit: '//cdnjs.cloudflare.com/ajax/libs/qunit/1.11.0/qunit.min',
    bootstrapDatepicker: '/js/bootstrap-datepicker',
    backboneValidation: '//cdnjs.cloudflare.com/ajax/libs/backbone.validation/0.7.1/backbone-validation-min'
  }

});

define([
    'jquery',
    'underscore',
    'backbone',
    'bootstrap',
    'qunit',
    'bootstrapDatepicker',
    'backboneValidation'
], function($, _, Backbone, Bootstrap, Qunit, Datepicker, backboneValidation){

    $(function(){

        var ReportModel = Backbone.Model.extend({
            validation: {
                'date.from': {
                    required: true,
                    msg: 'Please enter a from date'
                },
                'date.to': {
                    required: true,
                    msg: 'Please enter a to date'
                }
            },
            initialize: function(){
                console.log('test');
            }
        });

        $('#dp3').datepicker().on('changeDate', function(e){
            $('#toDate').datepicker('setStartDate', new Date(e.date.valueOf()));
        });
        $('#dp4').datepicker().on('changeDate', function(e){
            $('#fromDate').datepicker('setEndDate', new Date(e.date.valueOf()));
        });

    });
});
4

2 回答 2

5

使用垫片配置:http ://requirejs.org/docs/api.html#config-shim 。例子:

requirejs.config({
    paths: {
        jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min',
        underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
        backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min',
    },
    shim: {
        backbone: {
            //These script dependencies should be loaded before loading backbone.js
            deps: ['underscore', 'jquery'],
            //Once loaded, use the global 'Backbone' as the module value.
            exports: 'Backbone'
        },
        underscore: {
            exports: '_'
        },
    }
});
于 2013-05-07T08:12:30.130 回答
1

不幸的是,Backbone.js 没有启用 AMD,但你可以使用amdjs来做到这一点。

本教程将使用 Require.js 来实现模块化和有组织的 Backbone.js。

http://backbonetutorials.com/organizing-backbone-using-modules/

于 2013-05-07T02:49:38.660 回答