Hello I have a KnockoutJs computed observable ItemCountText in an extender "paged" that extends an observableArray. The observableArray is Products.
This works as expected, giving me "32 Products" (32, 48, whatever the number is, plus "Products" or whatever text I passed into the extender's settings)
<span data-bind="text:Products.ItemCountText"></span>
.. But this
<!-- ko with: Products -->
<span data-bind="text:ItemCountText"></span>
<!-- /ko -->
... is broken with Error: Unable to parse bindings. Message: ReferenceError: ItemCountText is not defined; Bindings value: text: ItemCountText
What might be the problem here that causes the extender's computed observable ItemCountText not to be defined within "with: Products" although a "Products.ItemCountText" binding is defined? It feels as though the binding mechanism for 'ko with:' cannot find its way 'into' the extender.
Here is a fiddle demonstrating the issue. To avoid distractions, the paged extender has everything snipped out that is not relevant to the issue.
To experience the issue described above, uncomment HTML line 14 of the fiddle and Run
Knockout is version 2.2.1
Here is the JsFiddle's HTML
<div id="bindMe">
<span data-bind='text: Yay()'></span>
<div data-bind="text: Products.ItemCountText"></div>
<div><span data-bind="text: Products().length"></span> on this page</div>
<!-- ko with: Products -->
<ul>
<!-- ko foreach: $data -->
<li data-bind="text: $data"></li>
<!-- /ko -->
</ul>
<!-- /ko -->
<span data-bind='text: Boo'></span>
<!-- ko with: Products -->
<!-- <div data-bind="text: ItemCountText"></div> -->
<div><span data-bind="text: length"></span> on this page</div>
<ul>
<!-- ko foreach: $data -->
<li data-bind="text: $data"></li>
<!-- /ko -->
</ul>
<!-- /ko -->
</div>
Here is the JsFiddle's Javascript
ko.extenders.paged = function (target, options) {
// Settings
var settings = $.extend({
"itemcounttext": 'Counted Items'
}, options);
target.TotalRecords = ko.observable();
target.ItemCountText = ko.computed(function () {
return target.TotalRecords() + " " + settings.itemcounttext;
});
return target;
};
/// -- End paging extender -- \\\
var ViewModel = function() {
var self = this;
self.Yay = ko.observable("Hello World... Products.ItemCountText.. it works great");
self.Boo = ko.observable("But if HTML line 14 is uncommented, why does ItemCountText from extender broke using ko with?");
self.Products = ko.observableArray([]).extend(
{
paged:
{
"itemcounttext": 'Products in total'
}
});
};
var myViewModel = new ViewModel();
ko.applyBindings(myViewModel, document.getElementById("bindMe"));
myViewModel.Products.push('prod a');
myViewModel.Products.push('prod b');
myViewModel.Products.push('prod c');
myViewModel.Products.TotalRecords(5);