boilerplatejs
boilerplatejs copied to clipboard
Implement Context.unsubscribe() method
Don't you feel the need for Context.unsubscribe()
method?
E.g. I want my component to listen for event only if it is active. So i call Context.listen()
in component's .activate()
function (imagine I don't use viewmodels). Currently there is no way to unsubscribe when the component is deactivated, meaning the event handler will be called anyway. But i don't want to handle this event when the component is hidden because e.g. it changes look&feel of component based on event data - it's a waste of resources.
You are correct here. Do you have an implementation already for it, which we may pull?
Meanwhile try not to depend on the events too much.. i believe it is much better to use URLController as much as possible as that will let you to bookmark and use browser back button.
I don't have implementation yet, though it's straightforward. Will prepare next week.
URLController
is not always applicable, e.g. several components working on the same page without any transitions. Imagine gmail-like application, with a StatusBar component listening for notifications from Inbox and Chat components to update its contents. When a new message arrives via ajax, there no URL change, it is just displayed in the inbox, but the status bar has to be changed to "now you have one more unread message".
Btw is unsubscribe the best name for this method?
You chose to have names in Context
different from their pubsub's counterparts, so maybe you'd like to continue:
- publish - notify
- subscribe - listen
- unsubscribe - ???
what are the alternatives? deafen
? unbind
? off
?
Imagine gmail-like application, with a StatusBar component listening for notifications You are spot-on here
Yes, publish, subscribe, unsubscribe style is a better use of names
do you mean we should rename Context.notify()
to Context.publish()
, etc? That would break backward compatibility...
Yes.. but BoilerplateJS is not intended to be used as a typical JS library, but as a reference architecture. It is just your own startup code.. you will be changing it a lot to suit your needs. When you do changes to that level, there is no simple way to upgrade to latest versions, unless someone do it manually. Therefore I dont think it has to be backward compatible.