Skip to content

Instantly share code, notes, and snippets.

@nilclass
Created March 29, 2013 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilclass/5269677 to your computer and use it in GitHub Desktop.
Save nilclass/5269677 to your computer and use it in GitHub Desktop.
Combining Ember and remoteStorage.js
var CONNECTED_INDEX_ROUTE = "index";
var CONNECTED_INDEX_ROUTE = "main";
// a function to initialize remoteStorage.js.
// I would call this from the "redirect" hook of each route,
// example see below.
function initializeRemoteStorage(route) {
return remoteStorage.claimAccess({
// <module> : <mode>
}).then(function() {
remoteStorage.displayWidget();
// if route object is given, transition when the state changes.
// (this conditional is required, because when this function
// gets called early through handleURL, there is no route
// object yet)
if(route) {
// (change to remoteStorage.on in version >= 0.7.2)
remoteStorage.onWidget('ready', function() {
route.transitionTo(CONNECTED_INDEX_ROUTE);
});
remoteStorage.onWidget('disconnect', function() {
route.transitionTo(DISCONNECTED_INDEX_ROUTE);
});
}
});
}
App.Router = App.Router.extend({
// overwrite handleURL, so it doesn't cause the app
// to die, when OAuth params are received through
// the fragment.
handleURL: function() {
try {
this._super.apply(this, arguments);
} catch(exc) {
// catch the exception being thrown when no route matches...
if(exc.message.match(/No route matched/) &&
// ... but only if the fragment could possibly be a
// query string:
document.location.hash.indexOf('=') > 0) {
// once remoteStorage has grabbed the params from the
// fragment, handleURL will be called again, because
// the fragment changes.
initializeRemoteStorage();
} else {
// different exception, -> keep on bubbling!
throw exc;
}
};
}
});
// declare a base route, initializing remoteStorage through
// the "redirect" hook.
App.BaseRoute = Ember.Route.extend({
redirect: function() {
initializeRemoteStorage(this);
}
});
// inherit all other routes from the BaseRoute, so the
// remoteStorage initialization happens from every entry
// point (if you have routes that shouldn't initialize
// remoteStorage, then of course you can still inherit
// from Ember.Route to get the default behaviour)
App.IndexRoute = App.BaseRoute.extend({
});
@galfert
Copy link

galfert commented Apr 2, 2013

I finally found a rather nice way to make it work with the Ember router. There actually is a method deferReadiness that tells the app to postpone starting the router until advanceReadiness is called.

Here is a little snippet that shows how to use it with remoteStorage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment