0

Having some issues with a function in Knockout.js. Basically it is a menu where the first menu item "Översikt" should fetch a JSON array and populate the view.

The knockout code:

self.ongoingAuctions = ko.observableArray([]);
self.getOngoingAuctions = function(data) {
    $.getJSON("assets/json/auctions.json", function(data) {
        self.ongoingAuctions(data);
    });
}

My click binding:

<a href="#" data-bind="text: 'Översikt', click: function(){ setHeadline.bind($data,'Översikt'); getOngoingAuctions() }, css: { active: 'Översikt' == headline() }" class="lead"></a>

The problem is that this only works the first time I click on the menu item. The JSON doesn't get fetched the second, third, n:th time.

What am I doing wrong? Or have I misunderstood something?

Thanks in advance!

Ismailp
  • 2,333
  • 4
  • 37
  • 66
  • Does it step into the getOngoingActions function or is the getJSON function called but returning cached data? http://stackoverflow.com/questions/13391563/how-to-set-cache-false-for-getjson-in-jquery – CyclingFreak Apr 08 '13 at 07:39
  • nothing is called at all when I look in the console. – Ismailp Apr 08 '13 at 07:41

1 Answers1

0

I have shared this fiddle for you that shows something else is wrong in your code that you havent not specified in the question:

It makes the call to the non existing json (in my case) every time you click

JS Fiddle to working code

var viewModel = function(){
    var self = this;
    self.ongoingAuctions = ko.observableArray([]);
    self.getOngoingAuctions = function(data) {
        $.getJSON("assets/json/auctions.json", function(data) {
            self.ongoingAuctions(data);
        });
    }
    self.setHeadline = function(){
        console.log('set headline')
    }
    self.headline = function(){
        console.log('headline');
    }
}

var myVm = new viewModel();

ko.applyBindings(myVm);
makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76
  • Hi, Thanks for the answer. You are right. There was something else wrong but I managed to solve it. Will update this Question. – Ismailp Apr 09 '13 at 18:07