Code blow doesn't work, and the error msg is:
Uncaught Error: Module name "text!templates/bookTemplate.html_unnormalized2" has not been loaded yet for context: _. Use require([])
define(['backbone', 'underscore', 'jquery'], function (Backbone, _, $) { var bt = require('text!templates/bookTemplate.html'); var BookView = Backbone.View.extend({ initialize: function () { this.render(); }, render: function () { var template = _.template(bt, { name: 'secret book' }); this.$el.html(template); } }); return BookView; });
then I move the "text!templates/bookTemplate.html" to define(), it works! below is working code:
define(['backbone', 'underscore', 'jquery', 'text!templates/bookTemplate.html'], function (Backbone, _, $, bt) { // var bt = require('text!templates/bookTemplate.html'); var BookView = Backbone.View.extend({ initialize: function () { this.render(); }, render: function () { var template = _.template(bt, { name: 'secret book' }); console.info('template', template); this.$el.html(template); } }); return BookView; }); // it is working
As my understanding, require() and define() is the same in loading a module. Is this correct? Can you help me explain why it works in define and doesn't in require()?