Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-domain support (JSON/P) #7

Open
grimen opened this issue Sep 12, 2010 · 3 comments
Open

Cross-domain support (JSON/P) #7

grimen opened this issue Sep 12, 2010 · 3 comments

Comments

@grimen
Copy link

grimen commented Sep 12, 2010

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?

@benpickles
Copy link
Owner

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
Copy link
Owner

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

@grimen
Copy link
Author

grimen commented Sep 24, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants