superdem
superdem

Reputation: 15

Reload knockout js view after ajax - json loaded

I am working on knockout,

This is static example:

static fiddle

This refresh when array is changed but;

This is dynamic example:

dynamic fiddle

This is not refresh and not load in init.

json part is here;

      $.getJSON( "/echo/json/")
      .done(function( json ) {
         $.each(json.data.data, function(i, item){
            console.log(item.name); // json Item
            data.push(JSON.stringify(item)); // I push the json item in array
            console.log(data); //array
          })        
  })

Thank you all

Upvotes: 0

Views: 719

Answers (1)

dfperry
dfperry

Reputation: 2258

Take a look here:

http://jsfiddle.net/KU9t5/4/

your getAttachments() function should just update the contents of the observable array:

ajax call:

$.post( "/echo/json/", "{}")
      .done(function( json ) {
          self.attachments([]);
         $.each(data, function(i, item){
            console.log(item.name); // json Item

            self.attachments.push(item);
            console.log(self.attachments()); //array
        });        
      })
      .fail(function( jqxhr, textStatus, error ) {
        var err = textStatus + ", " + error;
        console.log( "Request Failed: " + err );
    });

refresh() function:

self.refresh = function() {
    self.getAttachments();
  //self.attachments(self.getAttachments());
  //ko.mapping.fromJS(self.getAttachments(), self.attachments);
  console.log("attachments:" + self.attachments());

}

Upvotes: 1

Related Questions