Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js - How do load list or individual items from JSON on different routes?
    primarykey
    data
    text
    <p>I have just started working with Backbone.js by creating a simple portfolio site for myself. There is not much functionality at the moment but I hope to add more &amp; more as I become familiar with the library. I am using json to populate the data for the site which either lists all my portfolio items or displays one of them depending on the route. I have the list of portfolio items working/showing but am struggling to just show one item for a specific route (/#projects/2 for example), this is where I need help.</p> <p>I have tried to keep everything as simple as possible while I start learning Backbone &amp; have seen the various Boilerplates &amp; tutorials, none of which helped me hugely. </p> <p>At these routes, I would like this to happen:</p> <ol> <li>/ - list of portfolio items</li> <li>/#projects - list of portfolio items</li> <li>/#projects/3 - one portfolio item</li> </ol> <p>I have put my attempt into JSbin but it is not working because I don't know how to load the json properly...</p> <ul> <li><a href="http://jsbin.com/asezul/6/edit" rel="nofollow">http://jsbin.com/asezul/6/edit</a> to edit</li> <li><a href="http://jsbin.com/asezul/6/" rel="nofollow">http://jsbin.com/asezul/6/</a> to view</li> </ul> <p>I am certain my problem arises because I am loading all items into the collection rather than the model. Is that right?</p> <ol> <li><strong>How can I make this work?</strong></li> <li><strong>How can I improve this?</strong></li> </ol> <p><strong>[EDIT --------------------]</strong></p> <p><strong>After Peter's very kind help below, I am still in need of assistance. He said:</strong></p> <blockquote> <p>A view displays 1 model, a collection view displays a list of views, one per model.</p> </blockquote> <p><strong>So, how do I create a View to display one item from the json data &amp; then create the collection view to display all of them? The reason I ask is because I can return all them without much of a problem but returning a single item is proving quite tricky..</strong></p> <p><strong>I updated my code example too: <a href="http://jsbin.com/asezul/8/edit" rel="nofollow">http://jsbin.com/asezul/8/edit</a></strong></p> <p><strong>[---------------------EDIT]</strong></p> <p> </p> <p>Please let me know if you need more information, I am new to Backbone &amp; may have missed an important part that would help to answer my questions.</p> <p>Here's some code excerpts but I suggest viewing the code in JSbin:</p> <p><strong>The model:</strong></p> <pre><code>SITE.Project = Backbone.Model.extend({ initialize: function() { console.log(this, this.attributes.name); } }); </code></pre> <p><strong>The collection:</strong></p> <pre><code>SITE.Projects = Backbone.Collection.extend({ model: SITE.Project, url: 'content.json', parse: function(response) { console.log(response); return response.portfolio; } }); </code></pre> <p><strong>A view:</strong></p> <pre><code>SITE.ProjectsView = Backbone.View.extend({ el: '#main', initialize: function() { _.bindAll(this, 'render'); // create a collection this.collection = new SITE.Projects(); // Fetch the collection and call render() method var that = this; this.collection.fetch({ success: function () { that.render(); } }); }, template: SITE.helpers.template('project-list'), render: function() { $(this.el).html(this.template({ projects: this.collection.toJSON() })); } }); </code></pre> <p><strong>The router:</strong></p> <pre><code>SITE.Router = Backbone.Router.extend({ initialize: function() { this.el = $('#main'); }, routes: { "": "index", "projects": "projects", "projects/:id": "project" }, // Homepage / List of projects index: function() { var view = new SITE.ProjectsView(); }, // List of projects projects: function() { var view = new SITE.ProjectsView(); }, // Individual Project project: function(id) { var view = new SITE.ProjectView(); console.log("You are trying to reach project " + id); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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