I have been round and round with this one, and just cannot seem to make any progress. I am using the bootstrap-star-rating control found here
https://github.com/kartik-v/bootstrap-star-rating, and would like to use it within a template. No matter what incantation I try, I get a runtime javascript error indicating that the object does not support the property.
<script type="text/html" id="SearchResultTemplate">
<div>
<div class="row">
<div class="col-md-7 col-sm-3">
<h5 class="lblue" data-bind="html: businessName"></h5>
<span data-bind="html: backgroundSummary"></span>
<br/><br/>
<div data-bind="visible: averageRatingIsKnown()"><br/>Average Rating: <span data-bind="html:averageRating()"></span>
<input class="rating" data-readonly="true" data-bind="readonlyRating: averageRating(), size: 'xs'">
</div>
</div>
</div>
</div>
</script>
My custom binding code looks like this
ko.bindingHandlers.readonlyRating =
{
init: function(element, valueAccessor, allBindings, viewModel, bindingContext)
{
var value = ko.utils.unwrapObservable(valueAccessor());
var starSize = allBindings.get('size') || 'xl';
// Initialize the rating with the desired options
$(element).rating(); //{ size: starSize, disabled:true, showClear:false, showCaption:false });
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext)
{
// Get the data we are bound to...
var value = valueAccessor();
var valueUnwrapped = ko.unwrap(value);
var starSize = allBindings.get('size') || 'xl';
$(element).rating('refresh', { size: starSize });
$(element).rating('update', valueUnwrapped);
}
};
Any of the lines with $(element).rating(... throws the error.
The averageRating() method on my view model returns the value as expected.