Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  173] [ 6]  / answers: 1 / hits: 21344  / 11 Years ago, tue, october 22, 2013, 12:00:00

I am learning backbone js, trying to make a small project.



In the of te page, I load require.js and text.js from the cloudflare CDN



<script type=text/javascript src=//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.8/require.min.js>//</script>

<script type=text/javascript src=//cdnjs.cloudflare.com/ajax/libs/require-text/2.0.10/text.js>//</script>


I have made a backbone view called Boxes:



var Boxes = Backbone.View.extend({

// Choose an element.
el : '.content',

render : function () {

// Captur this -> the backbone view itself.
var that = this;

$(this.el).html('how do I load a html template here?');

}
});


Problems:




  1. When I add the text.js plugin to the page, I get the following error:



    Mismatched anonymous define() module: function (module) { 'use strict'; ......




So I can't have the require.js and the text.js both loaded, it gives me the above error even on a blank page without any other scripts on it.




  1. After I make the require js work with text js, how do I load an html template for that view?



Right now I know how to do it when I write my templates inline, in the index.html page.



I do it like this:



var Boxes = Backbone.View.extend({

el : '.content',

render : function () {

var that = this; // This backbone view

var template = _.template($('#user-list-template').html(), {});

that.$el.html(template);
}
});


Thank you!


More From » backbone.js

 Answers
27

In your HTML file, you only need to load requrejs like as shown in this index.html



<script type=text/javascript data-main=js/main src=js/libs/require-2.1.2.min.js></script>


In above, data-main tells requirejs where to load its bootstrap file and in this case, it is under js/main.js



You can find the file in here.



In the main.js file, you will need to specify



require.config({ ... });


to configure requirejs.



After that you can use define()/require() to load the templates like...



define(['text!../../templates/app/content.about.html'],...);


See here for a complete example.


[#74805] Monday, October 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
danalexc

Total Points: 114
Total Questions: 119
Total Answers: 103

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;