Reputation: 283
I am having a trouble displaying the model in an itemview. Currently using require.js with backbone.marionette.
this is my header template:
// header.js
define( [
"jquery",
"underscore",
"marionette",
"user",
"userSession",
"text!../../tpl/header_template.html"
], function ( $, _, Marionette, User, UserSession, HeaderTemplate ) {
return Marionette.ItemView.extend( {
template: HeaderTemplate,
initialize: function(){
alert(UserSession.firstName + " " + UserSession.lastName)
},
onRender: function(){
alert(UserSession.firstName + " " + UserSession.lastName)
},
model: new User({
lastName: UserSession.lastName,
firstName: UserSession.firstName
})
});
});
this is the template snippet code
Logged in as <%= firstName %> <%= lastName %>
and this the code when I try to display the itemview
// attached the view
layout.header.show(new HeaderView());
When I run this code, the view is rendered fine, but the model (firstname and lastname) data is not correct. firstname and lastname are both null. The weird thing is both on initialize and onRender, they both display the first name and last name correctly.
Does anyone know how to overcome this?
Upvotes: 1
Views: 833
Reputation: 2841
I think you should do like:
initialize: function(){
this.model = new User({
lastName: UserSession.lastName,
firstName: UserSession.firstName
});
}
Upvotes: 2