Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js + REST
    primarykey
    data
    text
    <p>Im very new to Backbone.js and have been following a couple tutorials to come up with the script below. What I am trying to achieve is retrieving JSON data from a rest api when my routes are used. If you look at the people function for the friends route you can see where im going with this. Where am i going wrong?</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;I have a back bone&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;button id="add-friend"&gt;Add Friend&lt;/button&gt; &lt;ul id="friends-list"&gt; &lt;/ul&gt; &lt;script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"&gt;&lt;/script&gt; &lt;script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"&gt;&lt;/script&gt; &lt;script&gt; Friend = Backbone.Model.extend({ name: null, age: null, }); FriendDetailModel = Backbone.Model.extend(); FriendDetailCollection = Backbone.Collection.extend({ url: 'slim/index.php/friends/', model: FriendDetailModel }); Friends = Backbone.Collection.extend({ initialize: function(models, options) { this.bind("add",options.view.addFriendLi); } }); AppView = Backbone.View.extend({ el: $("body"), initialize: function() { this.friends= new Friends(null, {view:this}); }, events: { "click #add-friend": "showPrompt", }, showPrompt: function () { var friend_name = prompt("Who is your friend?"); var friend_age = prompt("What is your friends age?"); var friend_model = new Friend({name: friend_name, age: friend_age}); this.friends.add(friend_model); }, addFriendLi: function(model) { $("#friends-list").append("&lt;li&gt;" + model.get('name') + " " + model.get('age') + "&lt;/li&gt;"); } }); var appview = new AppView; AppRouter = Backbone.Router.extend({ routes: { "friends":"people", "person/:name":"personDetail" }, people: function() { console.log('all the people'); var people = new FriendDetailCollection; people.fetch({ success: function(data) { console.log(data); } }, personDetail: function(name) { console.log('one person named ' + name); } }); var approuter = new AppRouter; Backbone.history.start(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>after running people.fetch Console.log shows</p> <pre><code>d _byCid: Object _byId: Object length: 4 models: Array[4] __proto__: x </code></pre> <p>If i do console.log(data.toJSON()); it returns </p> <pre><code>[] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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