0

So I have yet another DurandalJS question. So I have a few widgets that are pretty much self contained. They render or hide themselves depending on whether the current user is logged in or not e.g. I have a widget that displays the current users name, and another one that displays some setting for the current user. The 'current user is a value stored in local storage so everyone basically knows to get it from there and do their bit.

I have a security module which triggers an event on itself when a user is logged in and when a user is logged out.

All my widgets including shell require this security module and they all handle the event. Now I know the event is working because shell's event handler gets called but the widgets never see the event even thought they are displayed in shell.

However, if I do a hard refresh of the page (whether the user is logged in or out), all the widgets render properly so I know the widgets know what to do. Am I doing this wrong? If yes how best do I go about it.

Thanks

Obi
  • 3,091
  • 4
  • 34
  • 56
  • Assuming the security module returns a singleton this sounds like a viable approach. For some examples look at the event test specs https://github.com/BlueSpire/Durandal/blob/master/test/specs/events.spec.js – RainerAtSpirit Oct 30 '13 at 13:00
  • Hi, yes it returns a singleton, I think the problem was that I was also returning singletons from my widgets. I have no idea what the difference was but I never got the events when I returned singletons. I changed them to functions and once again all is right with the world :) – Obi Oct 30 '13 at 13:54
  • Durandal expects widgets to be constructor functions http://durandaljs.com/documentation/Creating-A-Widget/ so that it can instantiate multiple widgets of the same kind e.g a cell(td) widget. Could you please answer your own question and accept it in a couple of days. – RainerAtSpirit Oct 30 '13 at 17:12

1 Answers1

1

The problem was that I was returning singletons from my widgets. I have no idea what the difference was but I never got the events when I returned singletons. Durandal expects widgets to be constructor functions so that it can instantiate multiple widgets of the same kind see here

viewmodel.js is a function exported module that will serve as a location for all your widget's code. It will be bound to view.html by the widget infrastructure via the composition module.

Obi
  • 3,091
  • 4
  • 34
  • 56