Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot access all backbone model attribues from template
    text
    copied!<p>The problem I am trying to debug is that, in my backbone view, all the model attributes (name, id, email etc) are available, (I have console.log() the model attributes to verify this), however, when I render the view passing the model, only the name attribute is available in the template, all other attributes are undefined,please is there any thing I am missing, as this is my first backbone appliation and I have spent hours trying to debug this and have gone through many tutors online and my code seems correct, thanks </p> <p>//BACKbone view method(I have verified and all the model values are available here)</p> <pre><code>var profileView=Backbone.View.extend({ el:$('#content'), initialize:function(){ this.model.bind('change',this.render,this); }, render:function(){ var self=this; this.$el.html(_.template(profileTemplate,this.model.toJSON())); } }); </code></pre> <p>//HTML TEMPLATE (profileTemplate), only the name attribute is available in the template, //browser gives an error of 'undefined' for all other attributes except the name attribute</p> <pre><code>&lt;h1&gt;&lt;%=name.first%&gt; &lt;%=name.last%&gt; &lt;/h1&gt;//displays correctly &lt;%=email%&gt; //undefined </code></pre> <p>//SCHEMA</p> <pre><code> var AccountSchema=new mongoose.Schema({ email:{type:String,unique:true}, password:{type:String}, name:{ first:{type:String}, last:{type:String}, full:{type:String} }, }); </code></pre> <p>//IT IS SOLVED NOW, IT happened because I executed the fetch command on the model after calling the render method instead of before calling the render method</p> <p>This is my router, I call the model.fetch() from the router after the view is created. The problem stopped when I called model.fetch() before the rendering of the before</p> <pre><code>define([''views/profile''],function(,ProfileView){ var router = Backbone.Router.extend({ currentView: null, socketEvents: _.extend({}, Backbone.Events), routes: { 'addcontact': 'addcontact', 'index': 'index', 'login': 'login', 'register': 'register', 'forgotpassword': 'forgotpassword', 'profile/:id': 'profile', 'contacts/:id': 'contacts', "upload/:id":"upload" }, profile: function(id) { var model = new Account({id:id}); console.log('profile user:'+id); this.changeView(new ProfileView({model:model})); //View is created here model.fetch(); // model is fetched after view is rendered, cause for the problem in my case } }); return new router(); }); </code></pre>
 

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