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 changes on objects in multidimensional array #314

Closed laurensdijkstra closed 9 years ago

laurensdijkstra commented 9 years ago

Hi BorisMoore,

I'm investigating the capabilties of your library and it seems what I'm trying to achieve is not possible at this moment. I have a two dimensional array of objects within a project:

project.scenarios = [
  {
    instances: [{ ... }]
  },
  ...
];

What I would like is to listen to changes on propertyOfInterest within the objects in the instances objects. I have tried:

$.observe(project, "scenarios[].instances[].propertyOfInterest", ...);
$.observe(project, "scenarios[*].instances[*].propertyOfInterest", ...);

and a few different punctuations to no avail.

Is it not supported to directly listen to a specific property within a multidimensional array?

P.S. I know I could achieve the same functionality by using observeAll and checking the path for the propertyOfInterest.

BorisMoore commented 9 years ago

No, you have to use observeAll for that scenario.

For observe(data, path, callback) path can include a wild card, , but only as a leaf property - "a.b.", for example, (where a.b is an object, not an array) - to listen to any property change on the object a.b.

"a.b.*.foo" is not supported (neither when a.b. is an object nor when it is an array).

BorisMoore commented 9 years ago

Even after V1.1 this is unlikely to be supported, since the observeAll API is available, and the code to support "a.*.b" would be complex.

laurensdijkstra commented 9 years ago

Yes I can imagine that the fact that JS has no type-safety makes Arrays of Objects very unpredictable and would need alot of checks to determine whether an object somewhere at the requested depth in the path contains the requested property.

Thanks for the quick reply.