2014-11-21 94 views
1

我試圖用Grunt和grunt-contrib-requirejs插件優化Backbone項目。項目具有以下結構:Backbone + r.js:骨幹未定義

- root/ 
    |- public/ 
    | |- components/ 
    | |- js/ 
    | | |- login/ 
    | | | |- [dirs and files] 
    | | | |- main.js     
    | |- libs/ 
    |- gruntfile.js 

componentslibs目錄我有一個涼亭安裝庫。 gruntfile.js有以下選項requirejs:

requirejs: { 
    compile: { 
    options: { 
     name: "main", 
     baseUrl: "public/js/login/", 
     mainConfigFile: "public/js/login/main.js", 
     out: "public/js/app.build.js", 
     findNestedDependencies: true, 
    } 
    } 
}, 

bower.json我有以下相關:

"dependencies": { 
    "jquery": "2.0.3", 
    "jquery.cookie": "1.4.1", 
    "underscore": "1.6.0", 
    "backbone": "~1.1.2", 
    "backbone-validation": "~0.9.1", 
    "backbone-mediator": "~1.0.0", 
    "backbone.subviews": "~0.7.3", 
    "font-awesome": "~4.1.0", 
    "bootstrap": "~3.2.0", 
    "requirejs": "~2.1.14", 
    "require-handlebars-plugin": "~0.8.1", 
    "pace": "~0.5.5", 
    "modernizr": "~2.8.3", 
    "fastclick": "~1.0.2", 
    "moment": "~2.7.0", 
    "sweetalert": "~0.2.1" 
} 

最後,main.js有requirejs配置和主要的應用程序:

require.config({ 
    waitSeconds: 0, 
    hbs: { 
    templateExtension: 'html', 
    disableI18n: true, 
    disableHelpers: true 
    }, 
    shim: { 
    'jquery': { 
     exports: '$' 
    }, 
    'jqueryCookie': { 
     deps: ['jquery'] 
    }, 
    'underscore': { 
     exports: '_' 
    }, 
    'backbone': { 
     deps: ['underscore', 'jquery'], 
     exports: 'Backbone' 
    },  
    'backboneREST' : { 
     deps: ['backbone'] 
    }, 
    'backboneValidation' : { 
     deps: ['backbone'] 
    }, 
    'handlebars': { 
     deps: ['handlebars'], 
     exports: 'Handlebars' 
    } 
    }, 
    paths: { 
    jquery:    '../../components/jquery/jquery.min', 
    jqueryCookie:  '../../components/jquery.cookie/jquery.cookie', 
    underscore:   '../../components/underscore/underscore', 
    backbone:   '../../components/backbone/backbone', 
    backboneValidation: '../../components/backbone-validation/src/backbone-validation', 
    backboneSubviews: '../../components/backbone.subviews/backbone.subviews', 
    hbs:     '../../components/require-handlebars-plugin/hbs', 
    moment :    '../../components/moment/moment', 
    modernizr:   '../../components/modernizr/modernizr', 
    sweetAlert:   '../../components/sweetalert/lib/sweet-alert', 
    client:    '../../libs/client', 
    backboneREST:  '../../libs/backbone-rest', 
    viewManager:   '../../libs/view-manager', 
    errorManager:  '../../libs/error-manager', 
    } 
}); 

運行繁重的任務給我Done, without errors.消息,但是當我試圖加載結果腳本t在瀏覽器中,它給我Backbone is not defined錯誤。源地圖說它正在下跌Backbone.Validation。任何想法如何解決它?

回答

0

由於Backbone.validation擴展了Backbone,因此您應該在墊片上導出Backbone,這樣它就不會被覆蓋。

'backboneValidation' : { 
    deps: ['backbone'], 
    exports: 'Backbone' 
} 

希望它能解決您的問題。

1

幾天前我有同樣的錯誤。我修正了使用AMD版本(backbone-validation-amd-min.js)而不是標準版本。

+0

你能否詳細說明一下? – Robert 2015-03-02 15:11:12

+0

是的,這是兩個錯誤 - 我使用標準版本。另一個問題是我使用'define'函數而不是'requirejs'作爲我的應用程序的入口點。 – 2015-03-02 17:58:00