BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.68k stars 340 forks source link

Helper function depending on arrayChange and propertyChange #245

Closed BurnedToast closed 10 years ago

BurnedToast commented 10 years ago

Hi,

I want to build a application similar to a driver's log. So the user can enter:

In my model there is a array "driverLog" with multiple objects containing these informations. The user can edit, remove or add data.

At the bottom of this table I want to have a summary of the distance. That summary has to update when the user adds or remove objects from the array or when the user changes the distance-property.

So I wrote a helper function:

$.views.helpers({
  sumData: function(myArray) {
    var sum = 0;
    for (var i = 0; i < myArray.length; i++) {
      sum += myArray[i].distance;
    }
    return sum;
  }
});

This helper is getting called from my template like that:

{{:~sumData(~root.driverLog, ~root.driverLog.length)}}

It isworking for any changes to the driverLog (add / remove) itself, but if I just change the distance-value of any of these - the SUM wont get updated. I know the "depends" property for helpers - but I did not found any chance to return a path that makes the helper listening to the propertyChange-Events of the array-childs.

How can i manage this?

BurnedToast commented 10 years ago

Here is a simple jsFiddle showing my problem http://jsfiddle.net/jvwfmpya/6/

the sum is getting updated on "add" but not when changing the distance

BorisMoore commented 10 years ago

This is a JsViews issue, not a JsRender issue. Could you re-create this issue on JsViews? I'll reply to the new issue once you have created. Thanks! (BTW see this related issue on JsViews https://github.com/BorisMoore/jsviews/issues/158, and this related discussion on StackOverflow: http://stackoverflow.com/questions/25721180/summary-value-calculation-with-jsviews).

BurnedToast commented 10 years ago

here is the link for the reopened issue: https://github.com/BorisMoore/jsviews/issues/280