不定义主干要求JS [英] Backbone not defined with require js

查看:137
本文介绍了不定义主干要求JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到模型未定义的错误低于code。我已经添加下划线,jQuery的,骨干等,但我只能获得对象的jQuery的console.log函数返回仅 $

是否有一个原因,其他对象没有返回里面定义的功能?

下面是main.js我的code:

  require.config({
  路径:{
    jQuery的://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min',
    强调://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
    主干网://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min',
    引导://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 /引导-日期选择器,
    backboneValidation://cdnjs.cloudflare.com/ajax/libs/backbone.validation/0.7.1/backbone-validation-min
  }});定义([
    jQuery的,
    下划线,
    '骨干',
    '引导',
    qunit',
    bootstrapDatepicker',
    backboneValidation
]函数($,_,骨干,引导,Qunit,日期选择器,backboneValidation){    $(函数(){        VAR ReportModel = Backbone.Model.extend({
            验证:{
                date.from:{
                    要求:真实,
                    味精:'请输入起始日期
                },
                date.to:{
                    要求:真实,
                    味精:'请输入迄今为止
                }
            },
            初始化:功能(){
                的console.log('测试');
            }
        });        $('#DP3')。日期选择器()。在('CHANGEDATE',函数(E){
            $('#TODATE')日期选择器('setStartDate',新的日期(e.date.valueOf()));
        });
        $('#DP4')。日期选择器()。在('CHANGEDATE',函数(E){
            $('#寄件者)日期选择器('setEndDate',新的日期(e.date.valueOf()));
        });    });
});


解决方案

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

  requirejs.config({
    路径:{
        jQuery的://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min',
        强调://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
        主干网://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min',
    },
    垫片:{
        主干:{
            //这些脚本的依赖应该Backbone.js的加载之前加载
            DEPS:['下划线','jQuery的'],
            //一旦加载,使用全局的'骨干'作为模块值。
            出口:骨干
        },
        强调:{
            出口:'_'
        },
    }
});

I'm getting Model not defined error with below code. I've added underscore, jquery, backbone etc but I can only get object returned with console.log function for jquery only $

Is there a reason why other objects are not returning inside define function?

Below is my code from 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()));
        });

    });
});

解决方案

Use shim config: http://requirejs.org/docs/api.html#config-shim. Example:

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: '_'
        },
    }
});

这篇关于不定义主干要求JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆