I have a list of items with a toggle for each item respectively.
The user can enable/disable an item in the list by using the toggle for corresponding list item.
As and when the user is changing/toggling an item, I want to update that item in the list in my TS file.
And when the user clicks on Done button, I want to send that list to my server to be reflected in the database.
The way I plan to do is capture the toggle change using “ionChange” and pass in the “$event” and my “item” objects. As in the small code snippet below:
Aah…can do that? That came to my mind but wasn’t sure because that “level” is going to be an object in the list. Thanks!
The other reason I didn’t give it much thought or did not try it was because I wanted to set another flag “hasChanged” in that “level” object. This is because, it can be a big list and I don’t want to send across the whole list back. Using the “hasChanged” flag I can just filter the list and send the filtered list back to server.
How can I achieve it if I go your route?
Unless we’re talking at least tens of thousands of items, I think worrying about this is premature optimization.
That being said, if you insist, you could use something like lodash’s clone() method to prepare a “before” version of levelDetails, and then compare it to the “after” version modified by user actions at submission time to see which levels have changed.
Since you suggested the lodash approach, I am taking it for granted that my way of implementing it will be inefficient as compared to maintaining a copy of the original array using lodash’s clone().
I think so, especially if the list is big. The way you had it, you had to walk the entire list on every toggle flip. With clone-and-compare, that only has to happen once, after all toggles have been modified.
So the above functionality is on of the pages in my app. On the click of “Done” button (as in point #3 in my original post), instead of submitting/sending the list to server I want to go back to the previous page which I can do using “pop()” method, however, I don’t know how to carry this changed list back to the previous page.
I would stash it in a service provider injected by all relevant pages. You could also move the comparison and upload logic into there as well. Something with an API like:
@rapropos - that is a great solution! Thanks a lot!
How dumb of me not to come up with something similar. Maybe because I was thinking there has to be some cleaner and Ionic specific way. This is not to say your way is not cleaner, it’s just more work, so to say.
Apparently, after reading through forums and tutorial websites and documentation, Ionic doesn’t have a pop() method, like push(), in which we can pass objects/values. And nothing in the plans too, it looks like.
From a best practices perspective, what do you think of this solution?
Not a huge fan. I’m skeptical of explicit Promise construction in general, and I think that idea creates overly tight coupling between pages. Say a third page gets involved. It has to be initiated into the web of communication. When you let service providers do all the data handling, adding additional consumers (pages) is trivial and does not introduce any additional code complexity that must be tested and can introduce subtle bugs.