Knowing record change

Dear Aurelia-users/experts,

In the Contact Manager tutorial an example is given for the canDeactivate hook. In an application that I am developing, another record is displayed from several possible actions, not necessarily from showing another route. E.g. the users scans a barcode in an input field on top of the page and then the record is shown. The router-view does not only contain the details view as in the tutorial but contains the whole application.
My question is how I can discover or recognize that another record is shown whithout having to implement it in multiple locations. Does a hook exist that could help me?

Thanks in advance

1 Like

Not sure if I completely understand your question, but you can listen to router events with the aurelia Event aggregator (https://aurelia.io/docs/fundamentals/cheat-sheet#the-event-aggregator).
You can subscribe to:

  • router:navigation:processing
  • router:navigation:error
  • router:navigation:canceled
  • router:navigation:success
  • router:navigation:complete

If some actions you want to ‘listen’ to don’t trigger a route change, you could just fire an event yourself in your service layer, or perhaps listen to some specific http requests in a fetch interceptor, there are a lot of ways to tackle this issue

1 Like

Hello,

thanks for the fast reply.

I don’t always have a router event when another record is shown. Maybe I should?
For example, a page contains an input field where a number is typed in (search box) and then that record is shown on the page, without router navigation.

When a second search is done, the first should be saved to the server.

1 Like

Lets say the Input box is value bound to a barcode property. Now you can create a method called barcodeChanged(newValue, oldValue). This function will be automatically called by Aurelia once the property changes and you get access to the previous value

2 Likes

Hello zewa666,

it is not about the property barcode; that is just a search field. Based on the search field, details are displayed and can be edited. It is the change of the edited record that I want to discover when another record is displayed (new search or click on list of results, moving to another page,…). I want to save the changes on server. In my current implementation I do a save with change.delegate, but I want to do more on record base instead of individual fields. More or less what MS Access does when you edit a record

1 Like

You can try expression observer for that purpose https://codesandbox.io/s/x3qz6w2k6w

@bigopon What’s the difference between expression observer and property observer?

@krisc-informatica when I want to observe an entire object for changes, I mostly use something like this .
Beware though, this probably could degrade performance if you are working with large nested objects.

1 Like

The expression observer works with any arbitrary property path, example:

expressionObserver(obj, 'path.to.deep.property')

while property observer only works with a single level of property:

propertyObserver(obj, 'myProperty')
5 Likes

Hello,

Thanks for the suggestions, but I think I haven’t been clear enough in my question. The property observer or change.delegate etc are clear to me. What I want to know, is whether there an option to know when the record is ‘not shown anymore’ and then save any changes. In the tutorial, deactivate and canDeactivate are used for this. But I do not have a route change available.

1 Like

Can you create a small sample showing what youd like to achieve? I think we’re not fully getting the use case

1 Like

Sorry for going a bit off topic, but is there ever a reason for picking propertyObserver over expressionObserver?

1 Like

I think for what you want to do you can use observable properties and watch for changes. When a new search has completed, and a new value is set, you can save the old value. Does this help?

1 Like