Last active
September 22, 2017 22:24
-
-
Save golampo/b6fa76c9694e2fe90ac38c066a9e04f9 to your computer and use it in GitHub Desktop.
Cancelable prefetch - v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('parent', function() { | |
this.route('child'); | |
}); | |
}); | |
export default Router; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import prefetchUtil from '../../utils/prefetch'; | |
export default Ember.Route.extend({ | |
prefetch() { | |
return this.prefetched('parent').then((response) => { | |
// must check response for cancelation | |
if (prefetchUtil.isPrefetchCanceled(response)) { | |
return response; | |
} | |
return Ember.RSVP.resolve(Ember.assign({}, response, { | |
completeMsg: 'We made it!' | |
})); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import prefetchUtil from '../utils/prefetch'; | |
export default Ember.Route.extend({ | |
queryParams: { | |
cancel: { refreshModel: true } | |
}, | |
prefetch(params) { | |
// Using query params to cancel here for demo purposes | |
// In app this would be a check if user needs to be reauthenticated | |
if (params.queryParams.cancel) { | |
// In app we would also transition to login | |
// or to loading screen in case of external SSO | |
return prefetchUtil.cancelPrefetch({ | |
statusMsg: 'Canceled' | |
}); | |
} | |
return Ember.RSVP.resolve({ | |
statusMsg: 'Not Canceled' | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-prefetch": "2.1.6" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isPrefetchCanceled(data) { | |
return data.isPrefetchCanceled; | |
}; | |
export function cancelPrefetch(data) { | |
return Ember.assign({}, data, { | |
isPrefetchCanceled: true | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment