As you can see in post title I'm trying to use conditional block with condition of observableArray length. Unfortunately it's not working (actually when I check observableArray length it always shows 0) and error said:
Uncaught ReferenceError: Unable to process binding "if: function (){return usersets().length === 0 }" Message: usersets is not defined.
Here is the markup:
<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>
And this is hwi ViewModel looks:
<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);
}