Reputation: 67
I am trying to get id from the collection to get passed to the view. I am able to get the model's information, just can't get the view to grab the data.
In my template each line has the model id:
<td><a href="#/process/<%= model.id %>"><%= model.name %></a></td>
My router looks like this:
var Router = Backbone.Router.extend({
routes: {
'': 'welcome',
'process/:id': 'process'
}
});
var router = new Router();
router.on('route:process', function(id) {
console.log('home page loaded');
console.log(id);
var processing = new Processes({id:id});
processing.fetch({
success: function(model,response){
console.log(model.where({'id':id}))
}
})
processView.render({id: id});
});
The right id is coming through but I can't get the view to retrieve the id.
This is my first project using Backbone.js
Upvotes: 0
Views: 224
Reputation: 1019
The id / model attributes will not be available to you until the fetch (http://backbonejs.org/#Model-fetch) method call returns since it is an Ajax call done asynchronously.
Hence all actions that need to happen after data is fetched from the server should be placed inside the success / error (for error handling) callbacks.
In your case, the call to render view should be moved inside the success callback as bel.
processing.fetch({
success: function(model,response){
console.log(model.where({'id':id}))
processView.render({id: id});
}
});
Upvotes: 1