2013-08-20 74 views
2

requirejs配置:車把編譯瓦特/ RequireJS和BackboneJS

require.config({ 
    baseUrl: '/js/', 
    paths: { 
     jquery: './libs/jquery/jquery-1.10.1.min', 
     underscore: './libs/underscore/underscore-min', 
     backbone: './libs/backbone/backbone-min', 
     handlebars: './libs/handlebars/handlebars', 
     templates: '/templates' 
    }, 

    shim: { 
     underscore: { 
      exports: '_' 
     }, 

     backbone: { 
      deps: ['jquery', 'underscore'], 
      exports: 'Backbone' 
     } 
    } 
}); 

查看:

define([ 
    'backbone', 
    'handlebars', 
    'text!templates/mytemplate.html' 
], function(Backbone, Handlebars, Template){ 

    MyView = Backbone.View.extend({ 
     tagName: 'li', 
     template: Handlebars.compile(Template), 

     render: function() { 
      this.$el.html(this.template(this.model.toJSON())); 
      return this; 
     } 
    }); 

    return MyView; 
}); 

我遇到了以下錯誤:

Uncaught TypeError: Cannot call method 'compile' of undefined. 

回答

4

添加到您的墊片配置:

shim: { 
    handlebars: { 
    exports: 'Handlebars' 
    }, 
+0

你如何加載預編譯模板? – chovy

+0

@chovy,我使用requirejs handelbars插件 – ekeren

+0

有鏈接了嗎? – chovy

相關問題