I thought this to be an easy thing but I am stuck. Here is the code:
On Server:
app.get('/api/currentuser',function(req,res){
User.findOne({ _id: req.currentUser.id },'name',function(err, user_details) {
if (err) {
return next(err);
}
if (user_details) {
res.send(user_details);
} else {
return next(new NotFound('Could not find any such user'));
}
});
});
where req.currentUser.id is the id of logged in user from session. So if call /api/currentuser method, I am getting a response
{
_id: 6161616161619hjksk
name: 'Devesh Kumar'
}
Now in Backbone Marionette I am using a Model and an Item View to show the user name (and his _id and other details may be)
Backbone :
var CurrentUser = Backbone.Model.extend({urlRoot: '/api/currentuser' });
var CurrentUserView = Marionette.ItemView.extend({
template: "#current-user-view"
});
var currentuser = new CurrentUser();
var currentuser_view = new CurrentUserView({
collection: currentuserdetails
});
Completepp.rightRegion.show(currentuser_view);
Template:
<%= name %>
However, there is an error. It says name is not defined. When I set a CollectionView, and an ItemView then it can properly render. But in my case its not needed to set a collection
As a general question, how do I render such application level views in Backbone? Please Help