Last active
November 26, 2018 16:28
-
-
Save akatov/fe89af57bf61e3a9c526ea7f53a89817 to your computer and use it in GitHub Desktop.
Route Hooks
This file contains hidden or 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({ | |
appName: 'Ember Twiddle', | |
}); |
This file contains hidden or 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'; | |
const o = { | |
log(...args) { | |
this.send('log', this.routeName, ...args) | |
}, | |
/* | |
beforeModel(...args) { | |
this.log('hook', 'beforeModel', args); | |
return this._super(...args); | |
}, | |
model(...args) { | |
this.log('hook', 'model', args); | |
return this._super(...args); | |
}, | |
afterModel(...args) { | |
this.log('hook', 'afterModel', args); | |
return this._super(...args); | |
}, | |
redirect(...args) { | |
this.log('hook', 'redirect', args); | |
return this._super(...args); | |
}, | |
setupController(...args) { | |
this.log('hook', 'setupController', args); | |
return this._super(...args); | |
}, | |
renderTemplate(...args) { | |
this.log('hook', 'renderTemplate', args); | |
return this._super(...args); | |
}, | |
*/ | |
// this is currently broken | |
// serialize(...args) { | |
// this.log('hook', 'serialize', args); | |
// return this._super(...args); | |
// }, | |
actions: {}, | |
}; | |
[ | |
'beforeModel', | |
'model', | |
'afterModel', | |
'redirect', | |
'setupController', | |
'renderTemplate', | |
'activate', | |
'deactivate', | |
'didTransition', | |
'error', | |
'loading', | |
'willTransition', | |
].forEach(function(name) { | |
o[name] = function(...args) { | |
this.log('hook', name, args); | |
return this._super(...args); | |
}; | |
o['_' + name + 'Fired'] = Ember.on(name, function(...args) { | |
this.log('event', name, args); | |
}); | |
o.actions[name] = function(...args) { | |
this.log('action', name, args); | |
}; | |
}); | |
export default Ember.Mixin.create(o) |
This file contains hidden or 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('main', { path: 'main/:id' }, function() { | |
this.route('nested', { resetNamespace: true }); | |
}); | |
this.route('other'); | |
}); | |
export default Router; |
This file contains hidden or 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.Route.extend({ | |
model() { | |
return Ember.A(); | |
}, | |
setupController(controller, model) { | |
this._super(controller, model); | |
this.logs = model; | |
}, | |
actions: { | |
log(...args) { | |
console.log(...args); | |
this.logs.pushObject(args.join(' | ')); | |
}, | |
clear() { | |
this.logs.clear() | |
}, | |
transition() { | |
this.transitionTo('main', Ember.Object.create({ id: 10 })); | |
}, | |
} | |
}); |
This file contains hidden or 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 LoggingRouteMixin from '../mixins/logging-route'; | |
function resolveSoon(what) { | |
return new Ember.RSVP.Promise(resolve => { | |
Ember.run.later(null, resolve, what, 1000); | |
}); | |
} | |
function rejectSoon(what) { | |
return new Ember.RSVP.Promise((resolve, reject) => { | |
Ember.run.later(null, reject, what, 1000); | |
}); | |
} | |
const MainRoute = Ember.Route.extend({ | |
beforeModel(transition) { | |
// transition.abort(); | |
// return Ember.RSVP.reject(); | |
// this.transitionTo('other'); | |
// return resolveSoon(); | |
}, | |
model(params, transition) { | |
// transition.abort(); | |
const { id } = params; | |
// return resolveSoon(id); | |
return Ember.RSVP.resolve(id); | |
}, | |
afterModel(model, transition) { | |
// transition.abort(); | |
return new Ember.RSVP.Promise(resolve => { | |
Ember.run.later(() => { | |
resolve(); | |
// transition.abort(); // this breaks it | |
}, 1000); | |
}); | |
return resolveSoon(1); | |
// this.transitionTo('other'); | |
return rejectSoon(1); | |
}, | |
redirect(model, transition) { | |
// return Ember.RSVP.reject(); // this is ignored | |
// transition.abort(); | |
// Ember.run.later(transition, 'retry', 1000); | |
// this.transitionTo('other'); | |
}, | |
// this is currently broken | |
// serialize(...args) { | |
// const [model] = args; | |
// return { id: model.id }; | |
// }, | |
actions: { | |
refresh() { | |
this.refresh(); | |
} | |
} | |
}, LoggingRouteMixin); | |
// MainRoute.reopen(LoggingRouteMixin); | |
export default MainRoute; |
This file contains hidden or 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 LoggingRouteMixin from '../mixins/logging-route'; | |
export default Ember.Route.extend(LoggingRouteMixin); |
This file contains hidden or 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 LoggingRouteMixin from '../mixins/logging-route'; | |
export default Ember.Route.extend(LoggingRouteMixin); |
This file contains hidden or 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.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment