BorisMoore / jsviews

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

Feature request: possibility to make computed observable dependent on specific array elements #370

Closed trueqbit closed 7 years ago

trueqbit commented 7 years ago

Hi Boris,

I don't know how much of a need there is for such a feature, especially because it can be dealt with observing all properties. But I ran into this scenario and specifying an observing path to specific array elements would lower change tracking:

I have a data array with always at least one element. Certain parts of the UI always display/store back information from/to the first element. In my case it's an interval specification, where multiple interval points are equalized in regards to their time of day. So I'd like to express:

function computedTimeOfDay() {
    return this.interval.onset[0]._hour + ':' + this.interval.onset[0]._minute;
}
computedTimeOfDay.depends = ['interval.onset[0]._hour', 'interval.onset[0]._hour'];
// instead of
computedTimeOfDay.depends = 'interval.onset.**';
BorisMoore commented 7 years ago

I'm marking this as a feature request for after V1.1. But I may not implement it even then, since there are workarounds (the generic "**" path, or using observeAll) which address the scenario, and it may well be that implementing it would be too expensive in code-complexity, and may negatively impact performance in simpler scenarios.

The problem is that if you have an items array and you create a dependency path [1].foo, for example, then internally JsViews will add a listener for changes in the "foo" property of the items[1] object. Now, if you observably remove item[0], JsViews has to remove that listener and add a corresponding listener to the new item[1] - if there is one. Or if not, later, if you add a new item to the array (now item[1]) it has to add a listener to that object. So it gets messy...

See also https://github.com/BorisMoore/jsviews/issues/314