js-model icon indicating copy to clipboard operation
js-model copied to clipboard

Cross-domain support (JSON/P)

Open grimen opened this issue 14 years ago • 3 comments

After some rumbling I got js-model to work with cross-domain scripting - which is vey much a requirement for most web apps today. Not complete, but here's how the AJAX call should look like:

return jQuery.ajax({
        type: method,
        url: url + '.json',
        dataType: "jsonp",
        jsonpCallback: 'JSONP',
        data: data,
        dataFilter: function(data, type) {
          return /\S/.test(data) ? data : null;
        },
        success: function(data) {
          self.xhrComplete({status: 200, responseText: data}, 'success', model, callback)
        },
        error: function(xhr, textStatus) {
          console.error(xhr)
        }
      });

As you can see I'm trying to fake a XHR-object here because XHR is not really used for JSONP-requests under the hood (using script tags hack), and "complete" callback don't appears to be triggered by jQuery for JSONP-requests. I'm hardcoding 200 status here which is a bit ugly I guess, but as I understand it JSONP-requests work a status code is not available - just error/success.

So personally I need this hack for my own and I bet plenty of js-model users will end up needing it as well. Is it of interest?

grimen avatar Sep 12 '10 23:09 grimen

sorry thought i'd replied earlier, certainly that's of interest :)

wondering whether it would be a separate persistence adapter as it would probably be read only?

benpickles avatar Sep 16 '10 21:09 benpickles

i've pushed a branch with a JSONP persistence adapter (read-only). it's very simple and does the job and will probably get into master when i add some tests.

http://github.com/benpickles/js-model/blob/jsonp/src/model_jsonp.js

benpickles avatar Sep 23 '10 17:09 benpickles

No problem, I've been very busy anyways. Good look!

grimen avatar Sep 24 '10 14:09 grimen