Knockout.js cannot map observable array from json


last name

Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ).

function Movie(data) {
    this.name = ko.observable(data.name);
    this.description = ko.observable(data.description);
    this.duration = ko.observable(data.duration);
    this.id = ko.observable(data.id);
    this.imdb_id = ko.observable(data.imdb_id);
    this.original_name = ko.observable(data.original_name);
    this.poster = ko.observable(data.poster);
    this.type = ko.observable(data.type);
    this.year = ko.observable(data.year);
}

function MovieListViewModel() {
    // Data
    var self = this;

    self.moviesArray = ko.observableArray([]);
    self.searchQuery = ko.observable();

    self.searchMovies = function () {
        $.getJSON("/api/v1/movies/search/", {"query": self.searchQuery }, function(allData) {
            var mappedMovies = $.map(allData.movies, function(item) { return new Movie(item) });
            console.log(mappedMovies); // in this line output: [Movie, Movie, Movie, Movie, Movie, Movie]

            self.moviesArray(mappedMovies);
            console.log(self.moviesArray); // in this line output: []
        });    
    };
}

ko.applyBindings(new MovieListViewModel());

I don't understand what is wrong.

PS sorry for my english

brigade

This

{"query": self.searchQuery }

should

{"query": self.searchQuery() }

This

console.log(self.moviesArray)

should

console.log(self.moviesArray())

Related


Knockout.js cannot map observable array from json

last name Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ). function Movie(data) { this.name = ko.observable(data.name); this.description = ko.observable(data.description); this.duration = k

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Create observable knockout array from JSON response

Zonus C#/ MVC / Knockout / JSON I have the following javascript: function Feed(data) { this.ID = ko.observable(data.ID); this.RSSName = ko.observable(data.RSSName); alert(data.RSSName + " " + data.ID); } function ViewModel() { self = this;

Mapping JSON to Knockout observable array

Mohsin JK First Web Service: (User) [{id: 1, username: "Jhon"}] Second web service: (message) [{id: 34, message: "This is a message 1", UserId: 1}, {id: 35, message: "This is a message 2", UserId: 1} How can I map above the two ko.obervableArrays so that I can

Mapping JSON to Knockout observable array

Mohsin JK First Web Service: (User) [{id: 1, username: "Jhon"}] Second web service: (message) [{id: 34, message: "This is a message 1", UserId: 1}, {id: 35, message: "This is a message 2", UserId: 1} How can I map above the two ko.obervableArrays so that I can

Knockout JS: Pass observable array as parameter

trunk I have a 2d observable array called textFields: var t1 = ko.observableArray([{val: 0}, {val:0}]) self.textFields = ko.observableArray([t1]); I have a function called Solve that should take one parameter: self.solve = function(arr){ console.log(arr); }

Knockout JS: Passing observable array as argument

tomet I have a 2d observable array called textFields: var t1 = ko.observableArray([{val: 0}, {val:0}]) self.textFields = ko.observableArray([t1]); And I have a function called solve that is supposed to take an argument: self.solve = function(arr){ console.log

Knockout: Cannot map computed observable after Ajax call

arame3333 I have a viewmodel with an Ajax call to hold data: ViewModel = function (data) { contractsAutocompleteUrl = data.ContractsAutocompleteUrl; var self = this; ko.mapping.fromJS(data, lineMapping, self); self.save = function() { self.isB

Bind Asp.Net MVC json result to knockout.js observable array

Farhad Jameel I have some hardcoded values in my asp.net mvc controller. The GetPlanList()return JsonResultthis is to read in the viewmodel.js file and assign it to the ko.observableArray()data and then bind it to the table. The problem I'm having is how do I

Bind Asp.Net MVC json result to knockout.js observable array

Farhad Jameel I have some hardcoded values in my asp.net mvc controller. The GetPlanList()return JsonResultthis is to read in the viewmodel.js file and assign it to the ko.observableArray()data and then bind it to the table. The problem I'm having is how do I

Knockout Observable Array of Observable Array

Craig I have passed a model from .Net to my view model which is an object with several fields and a list, each field has another list. So, an object with a list of lists. In my view, I represent this as some data, with a list of tabs (the first list), and then

Knockout mapping Json Array to Observable Array getting error

Andreas Klumpe I'm having trouble mapping a json array to an observable array. I get an error "Cannot read property 'fromJSON' of undefined" Here is the knockout script I linked: Knockout 3.2.0.js Knockout.mapping-latest.js Knockout.mapping-latest.debug.js My

Knockout mapping Json Array to Observable Array getting error

Andreas Klumpe I'm having trouble mapping a json array to an observable array. I get an error "Cannot read property 'fromJSON' of undefined" Here is the knockout script I linked: Knockout 3.2.0.js Knockout.mapping-latest.js Knockout.mapping-latest.debug.js My