【发布时间】:2014-08-27 10:03:25
【问题描述】:
我正在尝试理解淘汰赛 3.2 组件,但我被卡住了。
我有一个组件“客户”
ko.components.register("customers", {
viewModel: function (params) {
var self = this;
this.customers = ko.observableArray();
this.selectedCustomerId = ko.observable(1);
this.selectCustomer = function (data) {
selectedCustomerId(data.Id);
};
$.getJSON('http://localhost:49435/Customer/GetCustomers', this.customers);
},
template: "<div><table class=\"table table-condensed table-responsive\"><thead><tr><th>Customer ID</th><th>Name</th><th>City</th></tr></thead><tbody data-bind=\"foreach: customers\"><tr><td data-bind=\"text: Id\"></td><td data-bind=\"text: Name, click: $root.selectCustomer\"></td><td data-bind=\"text: City\"></td></tr></tbody></table></div>"
});
但是在绑定的时候出现如下错误:
无法处理绑定"click:function(){return $root.selectCustomer }" 消息:无法读取属性 'selectCustomer' 未定义的
接下来我要做的是将selectedCustomerId 与另一个组件通信。这是否可能使用 PubSub 同步以及这如何可能。谁能给我一个提示从哪里开始。
【问题讨论】:
-
你会在任何地方调用 ko.applyBindings 吗?如果是这样,我认为作为参数传递给它的模型被认为是 $root。尝试使用 $parent 而不是 $root。
-
这是 ko 3.3 里程碑github.com/knockout/knockout/issues/1449
标签: knockout.js knockout-components