I’m wondering how to attach an event listener to a variable… I know that sounds super weird so here’s what I’m working with.
B.viewController = function() {
var view = 'large';
var update = function() {
if($(window).width() >= 481) {
var new-view = 'large';
return view == new-view
} else {
var new-view = 'mobile';
return view == new-view;
}
}
update();
$(window).resize(function() {
update();
});
};
now i’d like to expand this so that I can attach it as an event listener so for example I could use this to do something like coolImage.bind(‘B.viewController’, callback) So that if the view changes the callback is called without having to attach a resize listener to the window…
You may want to custom events to do the job.
Here is an example:
Publisher triggers an event when update is called, and Subscriber listens to the event when the event is triggered.
Applying this to your case, when
B.viewControlleris updated, you can callPubSub.trigger('view.updated')to trigger an update event. And then, havecoolImagelisten to the event byPubSub.bind('view.updated', coolImage.onViewUpdated).Credit/Reference