我正在尝试使用raty jQuery 插件在 KnockoutJS 中创建自定义绑定来为电影评分。比率插件的正常实现看起来像这样......
$('.raty').raty();
<div class="raty" data-rating="3"></div>
那会给你这样的东西......
<div class="raty" data-rating="3" style="cursor: pointer; width: 100px;">
<img src="/raty/img/star-on.png" alt="1" title="bad">
<img src="/raty/img/star-on.png" alt="2" title="poor">
<img src="/raty/img/star-on.png" alt="3" title="regular">
<img src="/raty/img/star-off.png" alt="4" title="good">
<img src="/raty/img/star-off.png" alt="5" title="gorgeous">
<input type="hidden" name="score" value="3">
</div>
我有一部observableArray
电影,收藏中的每部电影都有UserMovies.Rating
. UserMovies.Rating
当用户更新星级然后更新电影中的值时,我正在努力想出一种方法来处理 bindingHandler observableArray
。
这是我对实现和 bindingHandler 的尝试......
<div class="raty" data-bind="raty: UserMovies[0].Rating, ratyOptions: { cancel: true, half: true, size: 24 }"></div>
ko.bindingHandlers.raty = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var options = allBindingsAccessor().ratyOptions || {};
var value = ko.utils.unwrapObservable(valueAccessor());
// the line below throws Syntax error: Invalid label
ko.bindingHandlers.attr.update(element, { 'data-rating': value });
$(element).raty(options);
// how to fire upate function when user changes star ratings?
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
valueAccessor($(element).raty('score'));
}
};