Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:30:25+00:00 2026-06-08T20:30:25+00:00

The problem is that all Object attributes apart from ‘name’ call the error ‘id/url/whatever

  • 0

The problem is that all Object attributes apart from ‘name’ call the error ‘id/url/whatever is not defined’ in the console when accessed from the template. A template with just ‘name’ displays fine and shows the correct name, but as soon as I call a different attribute, eg. id or url, it breaks. The object passed to the view is a parsed static JSON file with all items sitting on the same level and accessible from the console with e.g. collectionName.models[0].get('id');

What has me confused is that the name attribute works, as if it is predefined somewhere in backbone/underscore code as a default.

Am I missing something very obvious?
Since I can access the model data from the console, I think that there’s something wrong with how the view itself handles the data, but I’ve tried rewriting it in a couple different ways and nothing seemed to make any difference.


All the relevant code.

Passed object format.
This is also what collectionName.models[0].attributes; returns in the console.

[{
"id":"0",
"name": "Building1",
"url": "building_1",
"floors":[{
    "id":"0",
    "name":"Ground Floor",
    "image":"",
    "rooms":[{
        "id": "r_1",
        "name": "Room 1",
    },
    {
        "id": "r_2",
        "name": "Room 2"
    }]
}
}]

}

Example template code:

<span class="name"><%= name %></span>
<%= id %> <%= url %>

The router code:

routes: {
  '': 'intro', // this route is using pretty much identical code and works fine, the model has the exact same format, the only difference is that all attributes work.
  ':id': 'firstLevel'    
},

firstLevel: function  (id) {
  window.singleBuilding = new ThisBuilding({}, {idBuilding: id});

  window.singleBuilding.fetch();      

  this.floorView = new FloorList({
    collection: window.singleBuilding
  });

  var $intro = $('#intro');
  $intro.empty();
  $intro.append(this.floorView.render().el);
}

Views:

window.FloorSingleList = Backbone.View.extend({

  className: 'floor-list',

  initialize: function  () {

  this.template = _.template(tpl.get('floors-list-item')); 
  _.bindAll(this, 'render');
  this.model.bind('change', this.render);
  this.testModel = this.model.attributes; // I tried passing the attributes directly to the templatewithout .toJSON(), which worked exactly the same, as in only the 'name' attribute worked
},

render: function  () {
  console.log("The test data is:", this.testModel);
  console.log("The actual model data is:", this.model);
  var renderedContent = this.template(this.model.toJSON());
  $(this.el).html(renderedContent);

  return this;
 }

});

window.FloorList = Backbone.View.extend({

tagName: 'section',
className: 'intro-list',

initialize: function () {

  this.template = _.template(tpl.get('intro-list'));
  _.bindAll(this, 'render');
  this.collection.bind('reset', this.render, this);
  this.collection.bind('change', this.render, this);
},

render: function  (eventName) {

     var $introList;
     var collection = this.collection;

  $(this.el).html(this.template({ }));
  $introList = this.$('.intro-list');
  collection.each(function (building) {
    var view = new FloorSingleList({
      model: building,
      collection: collection
    });
    $introList.append(view.render().el);
  });

  return this;
}

});

Model code:

window.ThisBuilding = Backbone.Collection.extend({

model : Building,

initialize: function(models, options) {
  // Initialising the argument passed on from the router.
  this.idBuilding = options.idBuilding;
  return this;
},

url : function(){
  return  "data.json"      
},

parse: function (response) {
  console.log("Passed parameters are :", this.idBuilding); // Returns the request parameters passed from the router.
  return response[this.idBuilding];
}

});

Templates & Bootstrap

// templates are loaded during the bootstrap 
tpl.loadTemplates(['header', 'intro-list', 'floors-list-item', 'building-list-item'], function() {
    window.App = new ExampleApp();
    Backbone.history.start();
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:30:27+00:00Added an answer on June 8, 2026 at 8:30 pm

    The problem is in how fetch in javascript is asynchronous…

    firstLevel: function  (id) {
      window.singleBuilding = new ThisBuilding({}, {idBuilding: id});
    
      window.singleBuilding.fetch();  // YOU FETCH HERE     
    
      this.floorView = new FloorList({
        collection: window.singleBuilding
      });
    
      var $intro = $('#intro');
      $intro.empty();
      $intro.append(this.floorView.render().el); // AND RENDER WHILE YOU CAN'T ASCERTAIN THE FETCH HAS BEEN COMPLETED...
    }
    

    So what happens is that the render tries to read a collection that hasn’t yet been initialized properly -> your models are not complete yet -> funny readings. Console log works with black magic when regarding to async operations. so it probably tells you something and the reality is something completely different So do this instead:

    firstLevel: function  (id) {
      window.singleBuilding = new ThisBuilding({}, {idBuilding: id}); 
    
      // Don't fetch here...   
    
      this.floorView = new FloorList({
        collection: window.singleBuilding
      });
    
      var $intro = $('#intro');
      $intro.empty();
      $intro.append(this.floorView.el); // Don't render here
    }
    

    And then in the FloorList-view:

    initialize: function () {
    
      this.template = _.template(tpl.get('intro-list'));
      _.bindAll(this, 'render');
      this.collection.bind('reset', this.render, this);
      this.collection.bind('change', this.render, this);
      this.collections.fetch(); // fetch here, your binds take care of rendering when finished
    }
    

    Update 2: Apparently I saw complexity where there was none… Ignore everything below

    From Backbone.js docs, on Model.toJSON()

    Return a copy of the model’s attributes for JSON stringification.

    So it returns the attributes as JSON. Now backbone defines id as

    A special property of models…

    A property, not an attribute. Same goes for url. In a javascript object that represents a backbone Model, the attributes are stored in their own object within the object and the id- and url-properties are stored elsewhere in the model-object. For example the javascript object representing your model could look something like this:

    {
      ...
      attributes: Object // this is where your name attribute lives
      ...
      id: 34, // this is your id property
      ...
      __proto__: ctor, // this is where your url-function resides
      ...
    }
    

    UPDATE: The id-property is embedded onto the JSON in model.toJSON()

    So when you do this.model.toJSON(), you create a JSON from the contents of your model-objects attributes-property and include the id-property. The url -property is not included in this. What you can do for example:

    var renderedContent = this.template({
      attributes: this.model.toJSON(),
      url: this.model.url()
    });
    

    and in the template

    <span class="name"><%= attributes.name %></span>
    <%= attributes.id %> <%= url %>
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (big) problem that all of you that work with Webforms might
Essentially, after compiling utility jars w/ ANT, I'm having the problem that all the
I'm trying to copy some text to xml file. The problem is that all
Seems to be a problem that many people have, but all the answers I
I am facing a design problem that I cannot figure out at all. I
The problem is that when I try doing multiple animations they all happen the
Thanks to all that responded to my previous thread. There is still a problem
I'm having a problem trying to create a Javascript function that checks all the
I got a little problem, I got a code, that reads all pixels and
In our application we have an object that receives attributes at runtime. For example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.