admin管理员组

文章数量:1336631

Lets say I would like to create a function that loops all the elements in an array (observableArray) and return the appropriate item from the array.

I was thinking of creating a puted function to handle this, and using ko.utils.arrayFilter to do the filtering.

Should I cache this action? Or does the puted or the arrayFilter do it internally?

Lets say I would like to create a function that loops all the elements in an array (observableArray) and return the appropriate item from the array.

I was thinking of creating a puted function to handle this, and using ko.utils.arrayFilter to do the filtering.

Should I cache this action? Or does the puted or the arrayFilter do it internally?

Share asked Aug 19, 2012 at 11:59 IgalStIgalSt 1,9842 gold badges18 silver badges26 bronze badges 2
  • The values of puted observables are cached. Their value is only calculated initially and whenever a dependency changes. So, you can access that puted's value over and over again and always receive the cached value. – RP Niemeyer Commented Aug 19, 2012 at 12:42
  • @RPNiemeyer thanks. Actually I would like to have the puted function to be based on arguments. Something like this one: jsfiddle/igalst/EcS27 – IgalSt Commented Aug 19, 2012 at 14:03
Add a ment  | 

1 Answer 1

Reset to default 9

The values of puted observables are cached. Their value is only calculated initially and whenever a dependency changes. So, you can access that puted's value over and over again and always receive the cached value.

Based on your ments, it sounds like you want to create puted observables based on certain arguments. A couple considerations with that technique:

  • The bindings for a single element are executed inside of a puted observable to track dependencies. That means that if you only want to use your filter in your UI, then you can actually avoid creating a puted observable and just call a filter function directly. Whenever a dependency changes, your bindings will fire and the function will run again. This would not be the best solution, if you want to also programmatically interact with the filtered data. Here is a sample: http://jsfiddle/rniemeyer/QSgPz/

  • If you are going to use this concept frequently, then you could even extend observableArrays to call the filter directly off of an observableArray like: http://jsfiddle/rniemeyer/VhCVc/

  • As an alternative, if you have a function that returns a puted itself, then you want to make sure that you are only calling it once for each filter puted that you need. You would not want to call it from a binding where it would get re-created each time the binding fired (unless you had a custom binding that handled it in the init properly). Here is a sample: http://jsfiddle/rniemeyer/JrHnT/. Again you could extend observableArrays to be able to create a puted for you if you would use this often.

So, if you are only using this from bindings, then you can choose to skip the puted and just use a function, as the binding uses its own puted observable. If you need to interact with it from your view model, then you would likely want to create the filters there.

本文标签: javascriptCache computed values with knockoutStack Overflow