Updated GORM Labs and JQuery PeriodicalUpdater

I’ve just released an update to GORM Labs and JQuery PeriodicalUpdater (which was introduced in this post). While the modifications to GORM Labs were improvements, the JQuery PeriodicalUpdater update ended up being a punt on some functionality which I thought was successfully implemented.

Specifically, the problem with JQuery PeriodicalUpdater is that I can’t seem to get at the text of the response via JQuery’s $.ajax callbacks. I’d like to store the response text and then check it against the previous response text, and only trigger the success callback if there was a change in that text. This is a requirement to deal with web APIs that oh-so-often completely neglect to send Not Modified if the result hasn’t been modified. If someone has some code that works for getting at the text of the response, I’d love to see it. And yes, I know about xhr.responseText and xhr.responseXML: the XHR isn’t directly accessible in the success callback, and this.xhr().responseText always gives the empty string and this.xhr().responseXML always gives undefined.

Details about the GORM Labs update can be found here and here. As always, the GORM Labs documentation is up-to-date. I’ve also shifted GORM Labs from WTFPL (popularized in Dear User of My Open Source Project) to CC0. I’m hoping that will make people (and their lawyers) a bit more comfortable.

BTW, I’ve gotten precisely zero feedback on my Cry for Help on Open Source Projects. Which is a bummer.

Related posts:

  1. JQuery PeriodicalUpdater (AJAX long polling/server polling)
  2. “Grails Persistence with GORM and GSQL” has gone public
  3. Cry for Help on Open Source Projects
  4. Updated TweetSpeak Again
  5. Taking a Bit of a Hiatus
This entry was posted in Open Source. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Bill

    I haven’t looked at your code but could you check the response text at the “complete” event if the status=200?

    I’m not sure if complete happens before success is raised but perhaps, if is is you could handle it this way

  • http://www.smokejumperit.com Robert Fischer

    Unfortunately, complete is called after success. And it also isn’t passed the parsed data, so I can’t just pick up all my logic and drop it into complete. I also tried the dataFilter callback, but 1) it’s only in the newest jQuery, which hinders adoption; 2) the data passed in there also doesn’t seem to be useful; and 3) the type parameter there isn’t set to the inferred types (*.xml), which is annoying.

  • http://www.smokejumperit.com Robert Fischer

    If I could get useful data out of complete, I suppose I could grab the parsed data in success, store it into an enclosed variable, and then use complete callback to check the data for duplication and only run the user-supplied callback if there’s a change…

    BTW, I’ve also tried leveraging the to-params function in jQuery and a variety of other serialization stunts, but none of them work particularly well.

  • http://cf-bill.blogspot.com Bill

    I just tried this:

    			function submitMyForm(){
     
    				$.ajax({
    					type: "GET",
    					url: this.action,
    					success: function(html){
    						console.log(html);
    					},
    					complete: function(htr){
    						console.log(htr);
    						return false;
    						if(htr.status == 200){
    							console.log(htr);
    						}
     
    					}
    				});        
     
    				return false;
    			};

    and the “complete” method does return to me the xmlHttpRequest object so I can fetch the responseText etc.. I’m not sure if this will help you at all – it was just a weak proof of concept.

    There is also this option:

    http://docs.jquery.com/Ajax/ajaxSuccess#callback

    Which I think gets executed everything an ajax requiest is completed successfully.. It has a method signature like so:

    function (event, XMLHttpRequest, ajaxOptions) {
      this; // dom element listening
    }

    Hope this helps
    Bill

  • http://www.smokejumperit.com Robert Fischer

    ajaxSuccess happens after success is executed, so that’s no good.

    In the first case, check htr.responseText: that was returning "" for me every time. And htr.responseXML was returning undef.

  • http://cf-bill.blogspot.com Bill

    Hrm, weird.. I put up a very rough demo here: http://oak.sbcs.com/demo/jqpost.html that shows that my “complete” is getting back an htr object.. You need firebug enabled on the panel to see the results since I’m using console.log()

  • http://www.smokejumperit.com Robert Fischer

    WTF? That’s working for me, too. This doesn’t, though: http://demo.smokejumperit.com/demo.html

  • http://cf-bill.blogspot.com Bill

    I dunno. I am using an older version of JQuery even (1.2.6)

    I think htr.responseXML will return null unless your response header indicates a content type of XML (http://www.w3.org/TR/XMLHttpRequest/#response-entity-body0). Since you are returning JSON I think responseXML will always be null.

    Scratch that – now I see you can optionally have it return XML so you probably already tested it that way as well.

    Now I realize I’m just bastardizing your code here but perhaps this will help:

     
    				var returnData;
     
    ajaxSettings.success = function(data){
    					returnData = data;
    				};
    				ajaxSettings.complete = function(xhr,success) {
    					if(success ==='success'){
    						var data = xhr.responseText.trim();
    						if(settings.success) { settings.success(returnData); }
    						timerInterval = settings.minTimeout;
    						if(callback) callback(returnData);
    						returnData = '';
    					}
     
     
    				};

    So basically I capture the parsed data in success to be returned in complete.. (this handles json parsing/xml, all that stuff that jquery does automatically) and gives you access to both it and the xhr..

    However, I don’t really notice this periodically updating after the inital page load (I copied your source.json and manually changed it while watching the page..

    In fact just to see if it would periodically run I updated getdata to this:

    				function getdata() {console.log('running'); $.ajax(ajaxSettings); }

    to show me that it was going to do it’s thing.. but I only ever see “running” logged one time.

    Anyway, hope this helps

  • http://www.smokejumperit.com Robert Fischer

    Yeah, I bet I broke the recall while trying to sort through things. It’s all just a bit of a mess right now.

    I tried the attempt you laid out there, but it wasn’t working for me. See the demo I linked to above: you can put a break in success and do xhr.responseText to see it. I’ll try it again, though: maybe I was just on crack.

    Thanks for helping me hash this out. I really appreciate it!

  • http://cf-bill.blogspot.com Bill

    Hrm. Well I don’t get the xhr in success; but in complete as I illustrated in my 7:35am post today. In success I get the nicely evaluated JSON object data which I store in a class level variable called “returnData” so that I have access to it in complete (because in complete you get the raw JSON string and not the evaluated object out of xhr).

    I’ll put my updated demo out on oak.sbcs.com – granted, this won’t have the recall stuff – but it should show you how it’s working for me.

    My demo polls “source.txt” instead of source.json becuase that server won’t serve file extensions that aren’t registered with it.

    http://oak.sbcs.com/demo/jqpost.html

    Bill

  • Pingback: JQuery PeriodicalUpdater Updated Again | Enfranchised Mind

  • http://www.smokejumperit.com Robert Fischer

    Thanks to this conversation, I got JQuery PeriodicalUpdater up and running again: http://demo.smokejumperit.com/demo.html

    Is there a good way to determine equality between JSON objects? (w/o add’l jQuery plugins)

  • http://cf-bill.blogspot.com Bill

    It almost works :O(
    On line 71 I get the following error in firefox:
    xhr.responseText.trim is not a function
    var rawData = xhr.responseText.trim();\r\n

    It seems string.trim() isn’t implemented for some reason. I get the same error in chrome as well.

  • http://www.smokejumperit.com Robert Fischer

    Odd: how is it working for me?

  • http://www.smokejumperit.com Robert Fischer

    Fixed.

  • http://cf-bill.blogspot.com Bill

    Excellent; now it’s working for me too. Good job!

  • http://www.smokejumperit.com Robert Fischer

    I wish GitHub reflected branches better. Branches don’t really fit into the conception that they’re presenting with their GUI.

  • Categories