Backbone错误无法调用方法Deferred

时间:2013-11-13 11:45:22

标签: javascript jquery backbone.js requirejs marionette

我收到错误'无法调用方法'延迟'在文件中未定义。 backbone.marionette.min.js中的错误:

为什么呢?感谢

配置:

require.config({

  baseUrl:"/static/js/portalApp/",

  paths: {
    jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min',
    underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min',
    backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min',
    marionette: '//cdnjs.cloudflare.com/ajax/libs/backbone.marionette/1.1.0-bundled/backbone.marionette.min',
    bootstrap: '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.2/js/bootstrap.min',
    templates: '/static/portalApp',

    // Plugins
    handlebars: '//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.1.2/handlebars.min',
    text: '/static/js/libs/text'






  },

    shim : {
    jquery : {
      exports : 'jQuery'
    },
    underscore : {
      exports : '_'
    },
    backbone : {
      deps : ['jquery', 'underscore'],
      exports : 'Backbone'
    },
    marionette : {
      deps : ['jquery', 'underscore', 'backbone'],
      exports : 'Marionette'
    },
    bootstrap: {
        deps: ['jquery']
    },

    handlebars:{
            "exports": "Handlebars"
    }




   }


});

require(["App", "routers/AppRouter", "controllers/Controller",  "jquery" , "bootstrap"],
    function (App, AppRouter, Controller) {
        App.appRouter = new AppRouter({
            controller:new Controller()
        });
        App.start();
    });

App.js

define(['jquery', 'backbone', 'marionette', 'underscore'],
    function ($, Backbone, Marionette, _) {
        var App = new Backbone.Marionette.Application();


        //Organize Application into regions corresponding to DOM elements
        //Regions can contain views, Layouts, or subregions nested as necessary
        App.addRegions({
            headerRegion:"header",
            mainRegion:"#main"
        });

        App.addInitializer(function () {
            Backbone.history.start();
        });


        return App;
    });

路由器

define(['backbone', 'marionette'], function(Backbone, Marionette) {
   return Backbone.Marionette.AppRouter.extend({
       //"index" must be a method in AppRouter's controller
       appRoutes: {
           "": "index"
       }
   });
});

控制器:

define(['App', 'backbone', 'marionette', 'views/HeaderView'],
    function (App, Backbone, Marionette, HeaderView) {

     return Backbone.Marionette.Controller.extend({
        initialize:function (options) {
            App.headerRegion.show(new HeaderView());
        },
        //gets mapped to in AppRouter's appRoutes
        index:function () {
            //App.mainRegion.show(new WelcomeView());
        }
    });
});

1 个答案:

答案 0 :(得分:3)

你得到的错误与Backbone没有找到jQuery一致。 Marionette source code恰好包含符号Deferred的两个实例。两次都是:

this._deferred = Marionette.$.Deferred();

Marionette.$初始化如下:

Marionette.$ = Backbone.$;

we've already established Backbone.$设置为jQuery的$符号。

因此,如果Backbone无法找到jQuery,则Backbone.$未定义且Marionette.$未定义且此未定义值不能使用Deferred方法。因此,“不能调用方法'延迟'未定义[...]”