3

I've been playing around with Knockout recently and I'm trying to replace a $("").change handler with a subscription to a ko computed observable.

The issue I have is that when I'm making an ajax call inside a computed observable with knockout, firebug doesn't break on the debugger keyword or any breakpoints inside the success callback, but works fine when the ajax is fired in the $().change handler.

The code appears to work though, but I'm interested to see if anyone knows why it doesn't stop at the breakpoint?

Here is the original code, which breaks on the success call back fine:

$("#textarea").change(function() {
                      $.ajax({ 
                            //code elided
                           success : function(response){
                                     debugger
                                     alert(response);
                              };
                       });
                 });

And here is the code in knockout.

function ViewModel(){
      var self = this;
      self.textValue = ko.observable();
      self.throttledValue = ko.computed(this.textValue)
                        .extend({ throttle: 400 });


      this.throttledValue.subscribe(function (val) {
          if (val !== ''){
              $.ajax({ 
                   //code elided
                   success : function(response){
                                 debugger
                                 alert(response);
                              };
                       });
           }
      });

};

I hope the above illustrates what I'm doing.

Essentially, both will display the alert with the response from the ajax call, so we know the success function is being called,but only the first one will stop on "debugger".

Can anyone shed light on this behaviour? is it something to do with it being inside the subscribe to the throttledValue computed observable?

Again, the code still works, just interested to see if anyone knows why the break point isn't being hit.

jflood.net
  • 2,446
  • 2
  • 21
  • 19

1 Answers1

1

I suppose that you making cross domain calls and they silently fails. I created sample with ajax call to the same domain, all works fine. Verified in Chrome, IE, FF

Try it http://jsfiddle.net/Hju2v/2/

Madman
  • 3,171
  • 2
  • 32
  • 44
  • 1
    Nope, definitely not cross domain? If it is silently failing, would the success call back still execute at all? As I said the alert is displayed so the callback is definitely executed. But your example proves that it's not a side effect of the knockoutjs. I'll have to keep looking into it. – jflood.net Feb 15 '12 at 02:30