Backbone没有使用require js定义

时间:2013-05-07 02:29:36

标签: javascript backbone.js requirejs

我正在使用以下代码获取模型未定义错误。我添加了下划线,jquery,骨干等但我只能通过仅用于jquery的console.log函数返回对象$

是否有其他对象未在define函数内返回的原因?

以下是来自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()));
        });

    });
});

2 个答案:

答案 0 :(得分:5)

使用shim config: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: '_'
        },
    }
});

答案 1 :(得分:1)

不幸的是,Backbone.js未启用AMD,但您可以使用amdjs来执行此操作。

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

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