使用具有observableArray长度的Knockout.js条件块时出错

时间:2014-10-15 04:05:41

标签: jquery knockout.js

正如你在帖子标题中看到的那样,我正在尝试使用条件为observableArray长度的条件块。不幸的是它不起作用(实际上当我检查observableArray长度它总是显示0)并且错误说:

  

未捕获的ReferenceError:无法处理绑定“if:function(){return usersets()。length === 0}”   消息:未定义usersets。

这是标记:

<div data-bind="foreach: usersets" id="user_sets">
            <!-- ko if: usersets().length === 0 -->
            <p id="noSets">Пока у вас нет личных комплектов. Напишите стилисту или просто добавьте подходящий комплект в избранное</p>
            <!-- /ko -->
            <div class="block">
                <input type="hidden" data-bind="value: $data.SetId" />
                <div class="fav" data-bind="css: { fullop: $data.IsFavorite == true }">
                    <img alt="" src="img/fav.png" data-bind="click: $root.setFavorite">
                </div>
                <div>
                    <img alt="" data-bind="attr: { src: $data.SetImg }">
                </div>
                <div class="txt">
                    <h3 data-bind="text: $data.SetName, click: $root.go"></h3>
                    <p><span data-bind="text: $data.ItemsNumber + ' вещей,'"></span><span data-bind="    text: ' общая цена ' + $data.SetPrice + ' руб'"></span></p>
                </div>
            </div>
        </div>

这是hwi ViewModel看起来:

<script>
        function SetsViewModel() {
            var self = this;
            self.usersets = ko.observableArray();
            self.setFavorite = function (data) {
                if (data.IsFavorite == true) {
                    action = "DELETE";
                    $.ajax({
                        type: action,
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        url: "/api/setlikes/" + data.SetId + "/" + $("#MainContent_guid").val(),
                        data: {},
                        success: function () {
                            window.location.reload();
                        },
                        error: function (xhr, status, error) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                }
                if (data.IsFavorite == false) {
                    var j = '{"UserID":"' + $("#MainContent_guid").val() + '", "SetID":"' + data.SetId + '", "IsFavorite": true}';
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        url: "/api/setlikes",
                        data: j,
                        success: function (data) {
                            window.location.reload();
                        },
                        error: function (xhr, status, error) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                }
            }
            self.go = function (data) {
                window.location.replace("http://www.prepp.me/set/" + data.SetId);
            }
            $.getJSON("/api/sets?username=" + $("#MainContent_guid").val(), self.usersets);
        }

1 个答案:

答案 0 :(得分:1)

您正在尝试访问用户组查询中的用户组,因为在foreach循环中,范围是当前的用户组项。

尝试

<!-- ko if: $root.usersets().length === 0 -->
//or
<!-- ko if: $parent.usersets().length === 0 -->