KnockoutJs 参数化 dependentObservable 数组
我有一个 viewModel 和一个从服务器更新的 observableArray,然后我需要能够为该 observableArray< 的过滤器定义动态数量的 div /em>。
这是一个 Scrum 板示例,想象一下您从服务器收到一系列故事。然后将其添加到 viewModel.stories() observableArray 中。
我希望为可观察数组中每个故事的属性的各种不同值提供模板绑定过滤器。
因此,鉴于
item.BoardState 是“Backlog”或“In Progress”,
我想要一个依赖的可观察量,我可以将其参数化为仅显示“In Progress”的故事
self.filterInProgress = ko.dependentObservable(function (filterParameter) {
return ko.utils.arrayFilter(self.stories(), function (item) {
console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
return ((item.BoardState === filterParameter));
});
});
不幸的是,它说它不起作用。任何建议都非常感激。
I have a viewModel with an observableArray that gets updated from the server, then I need to be able to define a dynamic number of divs to filters of that observableArray.
It's a scrum board example, So imagine you receive an array of stories back from the server. and you add the to viewModel.stories() observableArray.
I'd like to have template bound filters for various different values of a property of each story within the observable array.
So given
item.BoardState is "Backlog" or "In Progress"
I want a dependent observable that I can paramatize to only show stories that are "In Progress"
self.filterInProgress = ko.dependentObservable(function (filterParameter) {
return ko.utils.arrayFilter(self.stories(), function (item) {
console.log("Current Filter = " + filterParameter + "--- Current BoardState = " + item.BoardState);
return ((item.BoardState === filterParameter));
});
});
Unfortunately it says it doesn't work. any suggestions greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您始终可以将过滤器移出到单独的函数中,并为每个过滤器类型创建一个 dependentObservable:
You can always move the filter out to a separate function and create a dependentObservable for each filterType:
将参数移至视图模型并使用自定义绑定。所以你不会有任何问题。
看看这个例子,是我前段时间和我的大学讨论这个问题时诞生的。
演示 - http://jsbin.com/epojol/5
带预览的代码 - http://jsbin.com/epojol/5/edit#javascript,html,live
自定义绑定来自演示:
绑定html:
Move parameter to View-Model and use custom bindings. So You will have no problem with it.
Look for this example that were born while I discuss this question with my college some time ago.
Demo - http://jsbin.com/epojol/5
Code with preview - http://jsbin.com/epojol/5/edit#javascript,html,live
Custom binding from demo:
Binding in html:
我会这样写:
View:
ViewModel:
EDIT: now works as an ko.dependentObservable (ko.compulated in 2.0)
I would write it like this:
View:
ViewModel:
EDIT: now works as an ko.dependentObservable (ko.computed in 2.0)