-
-
Save chrism/0daf18b1377e5bcbdabaaef3fcbc0f2a to your computer and use it in GitHub Desktop.
Token schedule
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 Token from '../token'; | |
import { schedule as s } from '../token'; | |
export default Ember.Component.extend({ | |
init() { | |
this._super(); | |
this.token = new Token(this.routeToken); | |
}, | |
routeToken: null, | |
schedule(job) { | |
s(job, this.token); | |
}, | |
click() { | |
console.log('click'); | |
this.schedule(() => { | |
this.set('foo', 'bar'); | |
alert('component clicked'); | |
}); | |
} | |
}); |
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'; | |
import config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('my-route'); | |
}); | |
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'; | |
import Token from '../token'; | |
export default Ember.Route.extend({ | |
token: null, | |
model() { | |
this.token = new Token(); | |
return { | |
token: this.token | |
}; | |
}, | |
actions: { | |
willTransition() { | |
this.token.cancel(); | |
} | |
} | |
}); |
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
export default class Token { | |
constructor(parent) { | |
this._parent = parent; | |
this._cancelled = false; | |
} | |
get cancelled() { | |
return this._cancelled || (this._parent && this._parent.cancelled); | |
} | |
cancel() { | |
this._cancelled = true; | |
} | |
} | |
let nextTick; | |
let jobs = []; | |
export function schedule(job, parentToken) { | |
if (!nextTick) { | |
nextTick = setTimeout(() => { | |
let currJob; | |
while (currJob = jobs.pop()) { | |
currJob(); | |
} | |
nextTick = null; | |
}, 3000); | |
} | |
let token = new Token(parentToken); | |
let work = function() { | |
if (!token.cancelled) { | |
job(); | |
} | |
} | |
jobs.push(work); | |
return token; | |
} |
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.10.6", | |
"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.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment