javascript – 如何在knockoutjs中实现复选框依赖项

前端之家收集整理的这篇文章主要介绍了javascript – 如何在knockoutjs中实现复选框依赖项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一组复选框

>复选框A.
>复选框B.
>复选框C.

使用foreach数据绑定生成

  1. <input type="checkBox" data-bind="value: id,checked: $root.chkBoxSelected" />

从observableArray中获取其检查状态.所以检查一个框会将相应的值添加到数组中,标准的knockoutjs可以正常工作.然后我想添加一个简单的规则:

如果选中C,则还必须检查A和B.

在knockoutjs中添加这种逻辑的最简洁方法是什么?我尝试使用可写的可计算的observable:

  1. var viewmodel = {
  2. foo: observableArray(),..
  3. };
  4.  
  5. viewmodel.chkBoxSelected = ko.computed({
  6. read: function() {
  7. return this.foo();
  8. },write: function(value){
  9. //add it if not added already
  10. if($.inArray(value,this.foo()) < 0) {
  11. this.foo.push(value);
  12. }
  13.  
  14. // if C is present then A,B must be as well
  15. if($.inArray("C",this.foo()) >= 0) {
  16. if($.inArray("B",this.foo()) < 0) {
  17. this.foo().push("B");
  18. }
  19.  
  20. if($.inArray("A",this.foo()) < 0) {
  21. this.foo().push("A");
  22. }
  23.  
  24. }
  25. },owner: viewmodel
  26. });

在读写函数上放置一个断点:read被调用,页面加载正常.但是,当我单击任何复选框时,我收到以下错误(写断点永远不会被命中):

  1. knockout-2.0.0.debug.js:2297
  2.  
  3. Uncaught TypeError: Object function dependentObservable() {
  4. if (arguments.length > 0) {
  5. if (typeof options["write"] === "function") {
  6. // Writing a value
  7. var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined,it will default to "window" by convention. This might change in the future.
  8. options["write"].apply(valueForThis,arguments);
  9. } else {
  10. throw "Cannot write a value to a dependentObservable unless you specify a 'write' option. If you wish to read the current value,don't pass any parameters.";
  11. }
  12. } else {
  13. // Reading the value
  14. if (!_hasBeenEvaluated)
  15. evaluateImmediate();
  16. ko.dependencyDetection.registerDependency(dependentObservable);
  17. return _latestValue;
  18. }
  19. } has no method 'push'

解决方法

当检查的绑定绑定到数组时,它需要能够对它执行数组操作.因此,在这种情况下使用可写的计算可观察量将导致问题.

但是,您可以选择使用手动订阅来保持项目同步.

这是一个示例视图模型:

  1. var viewmodel = function() {
  2. var self = this;
  3. this.items = ko.observableArray([
  4. { id: "A" },{ id: "B" },{ id: "C" },{ id: "D" }
  5. ]);
  6. this.checked = ko.observableArray();
  7. this.checked.subscribe(function(newValue) {
  8. if (self.checked.indexOf("C") > -1) {
  9. if (self.checked.indexOf("A") < 0) {
  10. self.checked.push("A");
  11. }
  12. if (self.checked.indexOf("B") < 0) {
  13. self.checked.push("B");
  14. }
  15. }
  16. });
  17.  
  18. this.shouldBeDisabled = function(item) {
  19. return (item.id === "B" || item.id ==="A") && self.checked.indexOf("C") > -1;
  20. };
  21. };

这是观点:

  1. <ul data-bind="foreach: items">
  2. <li>
  3. <span data-bind="text: id"></span>
  4. <input type="checkBox" data-bind="attr: { value: id },checked: $root.checked,disable: $root.shouldBeDisabled($data)" />
  5. </li>
  6. </ul>

我使用attr:{value:id}而不是value来避免由值绑定附加的事件处理程序,因为值绑定旨在处理对字段的更改.在这种情况下,我们只想设置value属性.

此处示例:http://jsfiddle.net/rniemeyer/tQJMg/

猜你在找的JavaScript相关文章