我想了解淘汰赛3.2组件,但我卡住了。功能在淘汰赛组件(knockoutjs 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>"
});
但是绑定时,我得到以下错误:
Unable to process binding "click: function(){return $root.selectCustomer }" Message: Cannot read property 'selectCustomer' of undefined
我想要做的就是传达selectedCustomerId
到另一个组件接下来的事情。这可能使用PubSub同步,这是如何可能的。有人可以给我一个暗示从哪里开始。
你是否在任何地方调用ko.applyBindings?如果是这样,我认为传递给它的模型作为参数被认为是$ root。尝试使用$ parent而不是$ root。 – 2014-08-27 10:28:17
这是3.3版里程碑https://github.com/knockout/knockout/issues/1449 – huocp 2014-08-28 01:55:46