knockout.js - Knockoutjs initialize observableArray with data -
this way trying initialize observablearray data @ time declaration giving error typeerror: this.book not function
here jsfiddle link http://jsfiddle.net/jvqy8/11/
var books = [{book:"harry potter",author:"j.k rowling"},{book:"5 point someone",author:"chetan bhagat"},{book:"i had love story",author:"ravinder singh"}]; var appviewmodel = function() { this.firstname = ko.observable("amit"); this.books = ko.observablearray(books); this.updatebook = function() { this.book("harry potter , prisoner of azkaban"); } };
when try initialize observable array way this.books = ko.observablearray(books);
throwing error typeerror: this.book not function
but moment initialize observable array way this.books = ko.mapping.fromjs(books);
working fine.
so can tell me wrong way initialization code this.books = ko.observablearray(books);
could please me answer can understand difference between these 2 approaches below.
1) this.books = ko.mapping.fromjs(books);
2) this.books = ko.observablearray(books);
tell me happens internally when line execute this.books = ko.mapping.fromjs(books);
and happens internally when line execute this.books = ko.observablearray(books);
when use this.books = ko.observablearray(books)
, thing observable array itself, trying set property (like book
) if observable naturally fail.
when using this.books = ko.mapping.fromjs(books)
instead, each object in array , each field made observable, why this.book('new title')
works.
Comments
Post a Comment