PDA

View Full Version : extending controllers - expected behaviour?



delp
20 Mar 2014, 1:20 AM
Found an issue when extending controllers -
So I have ControllerA and in its init function I add a few listeners.

I then create ControllerB which extends ControllerA.
Now when I load the application it creates all of my controllers and runs through there init() functions as expected.

But as ControllerA was extended by ControllerB, ControllerA's init() function is called twice and adds the same event listeners twice - which in turn means that the function handlers for the events get called twice.
Is this expected behaviour? Should you just not extend a controller that you've created and instead use composition instead of inheritance?
So ControllerB would have a reference to ControllerA rather than extending it?

nohuhu
20 Mar 2014, 9:50 AM
delp,

Any code samples? A fiddle, even?

Regards,
Alex.

delp
20 Mar 2014, 3:03 PM
48385
I've create and attached a very simple demo - as its multi file demo I couldn't really demo in simple fiddler demo so put together a small project file.

if you load the zip file (which is an architect project) and run the app when you click on the grid the listener in the MyController will hit its function.
But it actually gets hit twice as I've also extended the MyController.js with MyControllerExtended.js

Hope this clears up what I'm meaning.
If I was to not extend the MyController.js class but reuse any of its functionality by use of composition then I can work around it. But I'm wondering if the example I've given is expected behaviour or not.

Cheers

nohuhu
25 Mar 2014, 11:02 AM
delp,

Looking at the code I don't see anything wrong - you're extending one controller from another but are not overriding the init() method, so naturally the extended controller registers for the same events as the base one. This is expected behavior as controllers are initialized by the application automatically when loaded.

One way to avoid this is to extend your controllers from a base class that does not have init() method defined. Another way would be to use mixins instead of base classes to share functional traits without introducing inheritance issues.

Regards,
Alex.

delp
26 Mar 2014, 4:30 PM
Yes I wasn't overriding the init, but that was my kind of my point. What's the best practise with controllers as I initially thought extending a controller could make sense, but due to the way they get created/initialize it would seem not. When we see what happens (registers the listeners twice), its obvious what its doing although it just seems wrong.
Definitely a case of extending a class when it not a suitable mechanism. As you've suggested mixins would be better.

I see nothing wrong in the way the controllers work, but possibly some kind of warning might be handy. Place a log warning into the ext dev file that could detect the attached code issues."Are you sure you want to extend this..." type of thing.
I sure there will be a use case for it, but why would you want a controller to register the same listener twice, it seems to me that in an app you only want 1 instance of a controller.