I am new to the concept of prototype inheritance and I have the following:
var dynamicPropertyOverview = new Tab($('#dynamicPropertyOverview'), $('.edit-search'));
dynamicPropertyController.init($('#dynamicPropertyOverview'), $('.edit-search'));
dynamicPropertyOverview.setUpPill();
$(window).resize(dynamicPropertyOverview.setUpPill.bind(dynamicPropertyOverview));
var dynamicPropertyReview = new Tab($('#dynamicPropertyReviews'), $('.edit-search'));
dynamicPropertyController.init($('#dynamicPropertyReviews'), $('.edit-search'));
dynamicPropertyReview.setUpPill();
$(window).resize(dynamicPropertyReview.setUpPill.bind(dynamicPropertyReview));
var dynamicPropertyMap = new Tab($('#dynamicPropertyMap'), $('.edit-search'));
dynamicPropertyController.init($('#dynamicPropertyMap'), $('.edit-search'));
dynamicPropertyMap.setUpPill();
$(window).resize(dynamicPropertyMap.setUpPill.bind(dynamicPropertyMap));
where my prototype function is as follow:
function Tab(page, pill) {
this.page = page;
this.pill = pill;
}
Tab.prototype.setUpPill = function () {
if(dynamicPropertyController.isDynamicPropertyOverview(this.page) && checkMobileAndTabletVisibility.isTabletView()){
dynamicPropertyController.addSearchModalMobileTrigger();
if(!pillView.isSearchBarNotHidden()){
pillView.hideSearchBarDisplayPill(this.page, this.pill);
}
}
else{
dynamicPropertyController.addSearchBarDesktopTrigger();
}
};
The problem I have is that each time a new instance is called:
1) var dynamicPropertyOverview = new Tab($('#dynamicPropertyOverview'), $('.edit-search'));
2) var dynamicPropertyReview = new Tab($('#dynamicPropertyReviews'), $('.edit-search'));
3) var dynamicPropertyMap = new Tab($('#dynamicPropertyMap'), $('.edit-search'));
is will always be overridden by the next one.
I thought that one way to get around this is to wrap each instance in a if statment like this:
if($('#dynamicPropertyReviews').length){
var dynamicPropertyReview = new Tab($('#dynamicPropertyReviews'), $('.edit-search'));
dynamicPropertyController.init($('#dynamicPropertyReviews'), $('.edit-search'));
dynamicPropertyReview.setUpPill();
$(window).resize(dynamicPropertyReview.setUpPill.bind(dynamicPropertyReview));
}
although it works I would like to understand if there is a better approach to solve this.
DYNAMIC PROERTY CONTROLLER ADDED:
var dynamicPropertyController = (function() {
var searchBarModalTrigger = 'js-open-mobile-search';
function isDynamicPropertyOverview(page){
return page.length;
}
function addSearchModalMobileTrigger(){
$('.edit-search').removeClass('display-searchBar').addClass(searchBarModalTrigger);
}
function addSearchBarDesktopTrigger(){
$('.edit-search').removeClass(searchBarModalTrigger).addClass('display-searchBar');
}
function init(page, pill){
if(page.length){
pillView.hideSearchBarDisplayPill(page, pill);
}
}
return {
isDynamicPropertyOverview: isDynamicPropertyOverview,
addSearchModalMobileTrigger: addSearchModalMobileTrigger,
addSearchBarDesktopTrigger: addSearchBarDesktopTrigger,
init: init
}
})();