BorisMoore / jsviews

Interactive data-driven views, MVVM and MVP, built on top of JsRender templates
http://www.jsviews.com/#jsviews
MIT License
855 stars 130 forks source link

Observe array of objects path #459

Closed alnico2001 closed 2 months ago

alnico2001 commented 4 months ago

When a new item/object is added to an array, and paths to specific properties are being observed...there are observe inconsistencies...

Observe works for number and ext but does not work for newly added phones object as phones needs to be observed too... $.observe(person,"phones.[].number", "phones.[].ext", changeHandler);

Therefore, this works for newly added phones object, but for number only....any additional paths using pattern: phones^[]... do not work (ext). $.observe(person,"phones^[].number", "phones^[].ext", changeHandler);

Here is one of your modified samples showing the issue: https://jsfiddle.net/alnico/8cgzwfnx/

To test: on load notice both number and ext are observed. Click "Add phone" and change number, it is observed...now change ext...it is NOT observed.

Note: if the 'Add phone' button doesn't work, then copy code into your local env...no idea why it's not working in jsfiddle.

BorisMoore commented 4 months ago

Thanks for bringing this up, Brent. I'll look into it...

BorisMoore commented 4 months ago

Hi Brent, can you test whether this candidate fix works for you?

jsviews.js.txt

alnico2001 commented 4 months ago

Thanks Boris...

The fix does not work in my app; it still only works on whichever path is first. I see see of course that it works in the test file ;-)

This is my actual code using view models:

$.observe(appVm, 
   "locations()^[].Address()",
   "locations()^[].City()",
   "locations()^[].State()",
   "locations()^[].Zipcode()", 

mobileAddressChange)
BorisMoore commented 4 months ago

Ah - interesting. Would you be able to provide me a small test example, with that pattern (or similar) observing view models? "locations()^[].Address()" (It will save me a bit of time :-) )

BorisMoore commented 4 months ago

Actually, I may be able to use this sample as a starting point: https://www.jsviews.com/#jsvviewmodelsapi@ismanagersample. I'll look into it. (Unless it is easy/rapid for you to come up with a test sample...)

alnico2001 commented 4 months ago

Here is an example I threw quickly together...it looks like the view model is causing the problem...

In this example file: only the first "phones()^[]...." works...even on initial rendering; never mind Add Phone...

$.observe(person,
   "phones()^[].number()",
   "phones()^[].ext()"
, changeHandler);

test_jsviews_observVM.txt

BorisMoore commented 4 months ago

Excellent. Thanks!,

BorisMoore commented 4 months ago

Hi Brent, here is a candidate, for you to test in your environment.

jsviewsIssue459b.js.txt

alnico2001 commented 4 months ago

This fix works...Thank you Boris!

BorisMoore commented 4 months ago

Good. These were in fact two different issues: "phones^[].number", "phones^[].ext" and "phones()^[]...

Thanks for calling them out!

BorisMoore commented 2 months ago

Resolved, and included in commit v1.0.14