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?
- 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
1 Answer
Reset to default 9The 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
版权声明:本文标题:javascript - Cache computed values with knockout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742404996a2468638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论