0

I have created a view in durandal for showing a list of products.

I want to be able to search those products, this is why I want to add a search form in a sidebar that will appear only for this products view.

The sidebar must be declared outside of the products view so it won't refresh with it whenever I change a filter in the search form. Means, I need an external sidebar that will refresh a view and will only appear when this view is activated.

I don't know how to start implement it and what is the best way to do it. Help would be great.

DorR
  • 645
  • 1
  • 9
  • 22

1 Answers1

0

There's a couple of decent approaches you can take here, but the best choice will depend on how you have allocated the sidebar area for the search form.

It comes down to how tightly coupled you want the products view model and search form to be.

Sharing the same view model (or part of it): if you can share the same KnockOut observables between both view models, then the rest is pretty much "business-as-usual". You can accomplish this by sharing a ko.observable between the products view model and the search form view model. This can be done in one the following ways:

  • sharing a common view-model base (here's some info on that: View Model inheritance when using Durandal)
  • create a separate module that both the products VM and search VM can require
  • set up the Durandal composition so that they both use literally the same view model. This is not very intuitive, however, and I wouldn't recommend it for this.

Once you have a shared model available, you can bind to it from both views, and KnockOut's observability should be able to provide all the feedback you need.

A more loosely-coupled approach is to use a pub/sub technique. This makes the modules pretty independent, but there are management and maintenance consequences associated, much like there are when using global variables. I would only recommend this when it is necessary keep the view models as isolated from each other as possible, or when the logistics just don't allow view models to know about each other.

In a pub/sub scenario, the search VM just publishes an event that the products VM is listing for, and vice-versa as necessary. There are many JS libraries that provide this functionality. It is even included as part of Durandal - here's some info on that: Durandal: Leverage Publish and Subscribe Also look at the Durandal Event reference

Community
  • 1
  • 1
Joseph Gabriel
  • 8,339
  • 3
  • 39
  • 53