Question:
I have a container called dashboard that loads different child views into it. All of the child views have a header element with an id of sub-header
What I want to do is update the class associated with that element from the parent dashboard JavaScript but since sub-header is not loaded yet I am having a hard time accessing it.
It was recommended that I look into using the _.defer function. Below is my example of code that is associated with the dashboard. Is there a way for me to check if the sub-header element has been loaded into the DOm yet?
Example:
onRender: function () {
var self = this;
this.resizeMenu(this.model);
this.listenTo(this.model, 'change:menuState', function () {
self.resizeMenu(self.model);
});
_.defer(function(){
console.log("Dashboard On Show Function");
if ($('#dashboardBody').hasClass('minimized')) {
$("#sub-header").removeClass("mobile-subheader");
$("#sub-header").removeClass("full-subheader");
$("#sub-header").addClass("minimized-subheader");
console.log("DashboardBody Minimized");
} else if ($('#dashboardBody').hasClass('mobile')) {
$("#sub-header").removeClass("full-subheader");
$("#sub-header").removeClass("minimized-subheader");
$("#sub-header").addClass("mobile-subheader");
console.log("DashboardBody Mobile");
} else if ($('#dashboardBody').hasClass('expanded')) {
$("#sub-header").removeClass("mobile-subheader");
$("#sub-header").removeClass("minimized-subheader");
$("#sub-header").addClass("full-subheader");
console.log("DashboardBody Expanded");
}
})
},
Results: I am seeing the console log that I am getting into the defer function and into the if statement but the header with the id sub-header is not being updated with the new classes.
Should I move this defer statement outside of the onRender function possibly into an onShow?