Here is the js:
var view = function(){
self.arry = ko.observable();
self.arry(ko.mapping.fromJS([{prop:'Test'},{prop:'Test'}]));
console.log(self.arry().length);
}
var v = new view();
The observable array length is always zero. How do I get the correct length?
Edit:
Updated the JS and fixed the errors.
http://jsfiddle.net/eRHTv/
var view = function(){
var self = this;
self.arry = ko.observableArray();
self.load_items = function(){
setTimeout(function(){
self.arry(ko.mapping.fromJS([{prop:'Test'},{prop:'Test'}]));
}, 100);
}
self.no_items_visible = ko.computed(function(){
return (self.arry().length == 0);
});
self.load_items();
ko.applyBindings(self);
}
var v = new view();
When you run this, no items div will always be visible and if you do self.arry = data, the view won’t get updated.
First thing is: You’re never defining the variable
self:Second: ko.mapping.fromJS() returns an observable array if the input is an array:
In total: