javascript - How to use Handlebars with RequireJS -
i encountered following error:
uncaught typeerror: cannot call method 'compile' of undefined. requirejs configuration:
requirejs.config({ baseurl: "resources", paths: { 'app':'lib', 'jquery': 'lib/jquery-1.9.1', 'bootstrap': 'lib/bootstrap', 'html5shiv': 'lib/html5shiv', 'spin': 'lib/spin', 'respond': 'lib/respond', 'underscore': 'lib/underscore', 'backbone': 'lib/backbone', 'handlebars': 'lib/handlebars-v3.0.3', 'template': 'app/templates' }, shim: { html5shiv: { deps: ['jquery'] }, respond: { deps: ['jquery'] }, bootstrap: { deps: ['jquery'] }, jquery: { exports: '$' }, underscore: { exports: '_' }, backbone: { deps: ['jquery', 'underscore'], exports: 'backbone' }, handlebars: { exports: "handlebars" } } }); require([ 'jquery', 'underscore', 'backbone', 'handlebars', 'app/router' ], function($, _, backbone, handlebars, router) { var router = new router(); backbone.history.start(); }); view:
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; });
shim libraries doesn't support amd. version of handlebars you're using supports amd , doesn't define global variable named handlebars. hence error. try removing handlebars config shim.
Comments
Post a Comment