Backbone.dualStorage
Backbone.dualStorage copied to clipboard
Connecting to not-origin
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.
A few suggestions:
- Make sure you set
$.cors=true;
- If you're using jQuery Mobile, set
$.mobile.allowCrossDomainPages = true;
in your config on the `mobilinit' event. - If you control the server, look into using
Access-Control-Allow-Origin
to allow xhr access to your app.
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.
Before you publicized this repository, I was working on something similar that might be helpful.
- I created an ApiConfig module that contains a
host
attribute (3 actually, based on which environment the app is running in). - I extended Models and Collections, adding two params -
service
andendpoint
attributes (host
could also be added here rather than in a config). - I set
Model.urlRoot
andCollection.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.