Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use require.js in my backbone project
    text
    copied!<p>I am new to backbone underscore and require.js. I followed <a href="http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/" rel="nofollow">this tutorial</a> to create a project using backbone.js and underscore.js .</p> <p>Then I want to add require.js to that project. This is what I modify in the theater.html :</p> <pre><code>&lt;body&gt; &lt;h1&gt;My Theater&lt;/h1&gt; &lt;script src="libs/require.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="main.js" type="text/javascript"&gt;&lt;/script&gt; &lt;div id="mainContainer"&gt;&lt;/div&gt; &lt;input type="button" value="Add Item" id="butAddItem" /&gt; &lt;script type="text/template" id="tmplt-Movies"&gt; &lt;ul&gt; &lt;/ul&gt; &lt;/script&gt; &lt;script type="text/template" id="tmplt-Movie"&gt; &lt;div&gt;*******************************************************&lt;/div&gt; &lt;div&gt;&lt;%= Id %&gt; &lt;/div&gt; &lt;div&gt;&lt;%= Name %&gt; &lt;/div&gt; &lt;div&gt;&lt;%= AverageRating %&gt; &lt;/div&gt; &lt;div&gt;&lt;%= ReleaseYear %&gt; &lt;/div&gt; &lt;div&gt;&lt;%= Url %&gt; &lt;/div&gt; &lt;div&gt;&lt;%= Rating %&gt; &lt;/div&gt; &lt;/script&gt; &lt;/body&gt; </code></pre> <p>I added fews line of code to main.js file :</p> <pre><code>require.config({ paths: { jquery: 'libs/jquery-1.7.1', underscore: 'libs/underscore', backbone: 'libs/backbone' } }); </code></pre> <p>Then I got 2 errors :</p> <pre><code>1. ReferenceError: Backbone is not defined , Theater.Models.Movie = Backbone.Model.extend({}); </code></pre> <p>This is main.js file : require.config({ paths: { jquery: 'libs/jquery-1.7.1', underscore: 'libs/underscore', backbone: 'libs/backbone' } }); </p> <pre><code> var Theater = { Models: {}, Collections: {}, Views: {}, Templates:{} }; Theater.Models.Movie = Backbone.Model.extend({}); Theater.Collections.Movies = Backbone.Collection.extend({ model: Theater.Models.Movie, url: "data/movies.json", initialize: function(){ console.log("Movies initialize"); } }); Theater.Templates.movies = _.template($("#tmplt-Movies").html()); Theater.Views.Movies = Backbone.View.extend({ el: $("#mainContainer"), template: Theater.Templates.movies, //collection: new Theater.Collections.Movies(), //Not needed initialize: function () { //_.bindAll(this, "render", "addOne", "addAll"); this.collection.bind("reset", this.render, this); this.collection.bind("add", this.addOne, this); }, render: function () { console.log("render"); console.log(this.collection.length); $(this.el).html(this.template()); this.addAll(); }, addAll: function () { console.log("addAll"); this.collection.each(this.addOne); }, addOne: function (model) { console.log("addOne"); view = new Theater.Views.Movie({ model: model }); $("ul", this.el).append(view.render()); } }); Theater.Templates.movie = _.template($("#tmplt-Movie").html()); Theater.Views.Movie = Backbone.View.extend({ tagName: "li", template: Theater.Templates.movie, //events: { "click .delete": "test" }, initialize: function () { //_.bindAll(this, 'render', 'test'); this.model.bind('destroy', this.destroyItem, this); this.model.bind('remove', this.removeItem, this); }, render: function () { return $(this.el).append(this.template(this.model.toJSON())) ; }, removeItem: function (model) { console.log("Remove - " + model.get("Name")); this.remove(); } }); Theater.Router = Backbone.Router.extend({ routes: { "": "defaultRoute" //http://localhost:22257/Theater/theater.htm }, defaultRoute: function () { console.log("defaultRoute"); Theater.movies = new Theater.Collections.Movies(); new Theater.Views.Movies({ collection: Theater.movies }); //Add this line Theater.movies.fetch(); console.log(Theater.movies.length); } }); var appRouter = new Theater.Router(); Backbone.history.start(); //This is a hack for demonstration purposes $("#butAddItem").click(null, function () { var movie = new Theater.Models.Movie( { "Id": "BVP3s", "Name": "Lord of the Rings: The Return of the King: Extended Edition: Bonus Material", "AverageRating": 4.3, "ReleaseYear": 2003, "Url": "http://www.netflix.com/Movie/Lord_of_the_Rings_The_Return_of_the_King_Extended_Edition_Bonus_Material/70024204", "Rating": "PG-13" } ); Theater.movies.add(movie); console.log(Theater.movies.length); }); </code></pre> <p>And i have no idea how to convert the main.js and create a app.js file for using require.js.</p> <p>Any idea please.</p> <p>Thank you so much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload