DurandalJS - Widget is not reinitialized on subsequent requests - durandal

This is an example of the problem: widget is not reinitialized on subsequent requests
I have a "pager" widget. The first time I navigate to a page where it is used, ctor is executed and the pager is initialized (page 1).
Now let say I navigate to page 6.
The problem is when I navigate to another page and then back to the page where the pager is, the pager (widget) is still rendered with page 6 selected and ctor is not executed. The widget controller is not a singleton.
Is there something I don't get?

Since the page is not re-rendered (so the widget), none of the widget's life cycle callbacks will be called. What you can do though it to communicate with widget from parent page's activate method by
Raise an event from root page's activate method which widget
listens
Update data in some observable which widget subscribes to
Or, dirty way - don't do it, just re render the widget by
wrapping widget with with/if binding which would change for each
activate method call. This may be helpful if you cannt change the
widget code, which is very unlikely.

Related

Dynamically loaded html doesnt have access to viewModel

I will give a simple case scenario to illustrate my problem. I have a view and a viewModel. The viewModel has a method redirect(). After my view and its model are loaded I dynamically add a html button to the page and use click.delegate to link to method redirect() in viewModel. But clicking the button doesn't call the method. How to fix this.
If binding is not a possible workaround
You cannot add new bindings to the DOM through DOM manipulation because they will not be bound properly.
Instead you should either conditionally show it -
<button click.delegate="redirect()" if.bind="showRedirect">Redirect</button>

Aurelia page life-cycle

I had a problem with Aurelia's binding system. I want so.e clarification on how view and viewmodels interact.
Lets say I have a div. This div has child divs that get generated by Aurelia's "repeat.for". I then have a function that does something to the parent div and its children. Where or when do I call this function. From experience, I cant call the function in the attached() hook because Aurelia's binding system is asynchronous so not all child divs may be ready by the time I call my function

UserControl that will notify viewmodel if user clicks anywhere outside of of it or it becomes no longer visible

I have an element that is connected to a viewmodel that uses MediaCapture to record audio. If the user is recording audio(presses the record button), and then navigates away from it (clicks somewhere else in the UI, switches windows etc...), I would like to stop the recording in my viewmodel.
I would like to be able to bind a dependency property on an element that will notify my viewmodel if the user clicks anywhere outside of the element or it becomes no longer visible to the user).
Is there and event inside a custom usercontrol that I can connect to a DP that will report what I'm looking for?
Specifically for page changes you can override the OnNavigatedFrom method of the page get the view model via
var viewModel = LayoutRoot.DataContext as MyViewModelType;
And then invoke your stop record method on viewModel ciewModel.StopRecording ()
For tapping other control or the recording control being moved off screen due to scrolling ..
I would implement and event aggregator which was known by pages and ui elements so any event they respond to (tap /scroll) that you wish to use as a trigger for making the recording stop would publish a "StopRecordingEvent" that your view model could subscribe and listen to the event aggregator for.
http://developingzack.blogspot.com/2012/09/what-why-and-how-event-aggregator.html?m=1

Layout to Layout Navigation Transitions going back to the wrong layout

I have two UICollectionViewControllers - lets call them master and slave. The master view controller is configured with layout A and the slave with layout B. I set useLayoutToLayoutNavigationTransitions on master to YES and push the slave onto the navigation stack. The layout changes from A to B and I can navigate < back and see layout A. All is good.
However, in response to the user typing some text into a search box in the master view controller, I need to change the layout of the master collection view controller to layout C. This works as expected, but when I push the slave collection view controller onto the stack (configured with layout B) and navigate < back, I end up back at the master collection view controller with layout A, not layout C, as expected.
So far, I've tried:
putting a symbolic breakpoint on -[UICollectionView setCollectionViewLayout:] (and it's related/sibling methods) to check whether something was messing with the collection view layout before pushing the slave onto the stack. I see the break point getting hit when I expect the layout to change, but nothing unusual in the process of popping the slave collection view controller off the navigation stack.
stopping in the debugger and verifying the type of the collection view's collectionViewLayout property and I can see it is set to an instance of layout C before pushing the slave collection view onto the stack. After navigating backwards, I can see the layout has returned to the type of layout A and not layout C (as expected), which tallies with what I'm seeing visually.
implementing -navigationController:willShowViewController:animated: delegate callback and setting the collection view layout to what's required. However this appears to get over-written.
implementing -navigationController:didShowViewController:animated: and setting the collection view layout, but this results in an infinite loop and errors logged to the console.
Does anyone have any ideas what's going on here and how to solve this? I'm wondering if it's a straight-up UIKit bug, and if so, if there are any potential workarounds.

Prism. OnNavigationTo doesn't fired when i show my view with RegisterViewWithRegion

I have a little problem with the OnNavigatedTo method in the INavigationAware interface.
When I show my view with RegionManager.RequestNavigate(myRegionName, myViewName),
the OnNavigationTo method is called.
But when I use RegionManager.RegisterViewWithRegion(myRegionName, typeof(myView))
I can not get this scenario, and after that, I call
RegionManager.RequestNavigate(myRegionName, myViewName2) to my second view i am having a call to OnNavigatedFrom method of my first view.
My question is:
Why OnNavigatedTo method does not called and how i can get notice about view is shown when i use RegisterViewWithRegion?
Registering using the region manager will show the first view that was registered with it. It will never call OnNavigatedTo. Basically, to get it to do what you want to do, you'll need to "navigate" to your first View without OnNavigatedFrom to be called. To do this:
// Register all your views into the region
// The first View that is registered is automatically activated
regionManager.Regions["myRegionName"].Add(myView);
regionManager.Regions["myRegionName"].Add(myView2);
// Deactivate the View so it doesn't show in the UI
regionManager.Regions["myRegionName"].Deactivate(regionManager.Regions["myRegionName"].ActiveViews.First());
// Now navigate to your first screen
regionManager.RequestNavigate("myRegionName", "myView");
OnNavigatedTo should be called once, and OnNavigatedFrom should only be called after you request navigation to another View.
To allow view navigation you have to register it as an object, try something like that:
_container.RegisterType<Object, MainView>("MainView", new TransientLifetimeManager());
_regionManager.RegisterViewWithRegion("MainRegion", () => _container.Resolve<MainView>());
the first line allow you view navigation while the second will automaticly resolve the view as the region is created