Backbone.dualStorage icon indicating copy to clipboard operation
Backbone.dualStorage copied to clipboard

Connecting to not-origin

Open lucian1900 opened this issue 12 years ago • 3 comments

Previously, there was a hack with a global URLs object to support connecting to something other than the origin.

We need something else to do the same, since it's a necessary feature for PhoneGap.

lucian1900 avatar Jan 27 '12 10:01 lucian1900

A few suggestions:

  1. Make sure you set $.cors=true;
  2. If you're using jQuery Mobile, set $.mobile.allowCrossDomainPages = true; in your config on the `mobilinit' event.
  3. If you control the server, look into using Access-Control-Allow-Origin to allow xhr access to your app.

kurtmilam avatar Jan 27 '12 13:01 kurtmilam

It already works without CORS on PhoneGap by whitelisting the server. We do control the server, but have little time to fix it atm. Thanks for the tips.

This issue is strictly about the URL models point to. I want to keep the host out of the url, but still allow connection to both origin and a different domain.

lucian1900 avatar Jan 27 '12 13:01 lucian1900

Before you publicized this repository, I was working on something similar that might be helpful.

  1. I created an ApiConfig module that contains a host attribute (3 actually, based on which environment the app is running in).
  2. I extended Models and Collections, adding two params - service and endpoint attributes (host could also be added here rather than in a config).
  3. I set Model.urlRoot and Collection.url to a function that builds the correct URL: apiConfig[environment].host + '/' + object.service + '/' + object.endpoint

This gives me a flexible way to dynamically build the Model.urlRoot and Collection.url. The setup could be fleshed out to cover more edge cases, but it works for my needs right now.

How this relates to dualSync. When creating a new store, dualSync could simply look for object.service and object.endpoint. If either or both are found, the key for the new store would be [object.service] + '/' + [object.endpoint] + '/' guid (square brackets represent optional values here). If neither are found, revert to object.url + '/' guid.

dualSync would still work for people who aren't interested in adding host, service or endpoint, since it would fall back to url, while still supporting those who want a little more granularity.

Another option is to pass the key/endpoint inside options, but I honestly don't like having to pack too much into options in order to have things work correctly.

kurtmilam avatar Jan 28 '12 14:01 kurtmilam