Last active
October 7, 2019 22:24
-
-
Save machty/f7a2c3b60502420a9e48a8c0edd9b279 to your computer and use it in GitHub Desktop.
New Twiddle
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 { task, timeout } from 'ember-concurrency'; | |
export default Ember.Controller.extend({ | |
myTask: task(function * () { | |
yield timeout(1500); | |
return { username: "machty" }; | |
}).restartable(), | |
otherTask: task(function * () { | |
yield timeout(1500); | |
return { username: "trekkles" }; | |
}).restartable(), | |
}); |
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 { task, timeout } from 'ember-concurrency'; | |
const { computed, inject } = Ember; | |
const State = Ember.Object.extend({ | |
tagName: '', | |
t: null, | |
task: null, | |
state: computed.or('task.state', '_defaultState'), | |
_defaultState: 'idle', | |
init() { | |
this._super(); | |
this.perform = Ember.run.bind(this, '_perform'); | |
}, | |
_perform(...args) { | |
// TODO: how to compose "performability" when | |
// we decorate the underlying task with timeouts? | |
if (!this.get('task.isRunning')) { | |
this.get('_internalTask').cancelAll(); | |
this.get('_internalTask').perform(...args); | |
} | |
}, | |
isActive: computed.bool('_lastTaskInstance.isRunning'), | |
isDisabled: computed('_lastTaskInstance', 'task.last.isRunning', function() { | |
let lastTaskInstance = this.get('_lastTaskInstance'); | |
let last = this.get('task.last'); | |
return last && last.get('isRunning') && last !== lastTaskInstance; | |
}), | |
successValue: null, | |
isSuccess: false, | |
cooldown: 3000, | |
_internalTask: task(function * (...args) { | |
let ti = this.get('task').perform(...args); | |
this.set('_lastTaskInstance', ti); | |
let value = yield ti; | |
try { | |
this.set('isSuccess', true); | |
this.set('successValue', value); | |
yield timeout(this.get('cooldown')); | |
} finally { | |
this.set('isSuccess', false); | |
this.set('successValue', null); | |
} | |
}), | |
_lastTaskInstance: null, | |
}); | |
export default Ember.Helper.extend({ | |
compute(_, options) { | |
return State.create({ | |
task: options.task, | |
t: options.t, | |
}); | |
}, | |
}); |
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 { task, timeout } from 'ember-concurrency'; | |
const { computed, inject } = Ember; | |
export default Ember.Helper.extend({ | |
i18n: inject.service(), | |
_stashedState: null, | |
state: null, | |
t: null, | |
default: computed('state.state', 'state.isDisabled', 'state.isSuccess', function() { | |
let state = this.get('state'); | |
if (state.get('isSuccess')) { | |
return this._findFirstTranslation([ | |
'success', | |
'idle' | |
], { value: state.get('successValue') }); | |
} else if (state.get('isDisabled')) { | |
return this._findFirstTranslation(['idle']); | |
} else { | |
return this._findFirstTranslation([ | |
state.get('state'), | |
'idle' | |
]); | |
} | |
}), | |
idleText: computed(function() { | |
return this._findFirstTranslation(['idle']); | |
}), | |
_findFirstTranslation(keys, data = {}) { | |
let prefix = this.get('t') || 'button.default'; | |
let i18n = this.get('i18n'); | |
for (let i = 0; i < keys.length; ++i) { | |
let key = `${prefix}.${keys[i]}`; | |
if (i18n.exists(key)) { | |
return i18n.t(key, data); | |
} | |
} | |
// TODO: better messaging | |
return `Missing Translations (${keys.join(', ')})`; | |
}, | |
compute(_, { state, t }) { | |
this.set('state', state); | |
this.set('t', t); | |
return this; | |
}, | |
}); |
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 default { | |
'button': { | |
'default': { | |
'idle': 'Go', | |
'running': 'Please Wait...', | |
}, | |
'add_user': { | |
'idle': 'Add a user', | |
'running': 'Adding User...', | |
'success': 'Added User {{value.username}}!', | |
}, | |
'save_profile': { | |
'idle': 'Save Profile', | |
'running': 'Saving Profile...', | |
} | |
}, | |
'loading-banner': { | |
'idle': 'Add a user', | |
'running': 'Adding User...', | |
'success': 'Added User {{value.username}}!', | |
} | |
}; |
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
body { | |
margin: 12px 16px !important; | |
} | |
.button { | |
position: relative; | |
} | |
.button.active::before { | |
content: ' '; | |
display: block; | |
position: absolute; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
border: 6px solid rgba(255,255,255,0.4); | |
} | |
table .button { | |
width: 100%; | |
margin: 0; | |
} |
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.10.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.6.0", | |
"ember-data": "2.6.1", | |
"ember-template-compiler": "2.6.0" | |
}, | |
"addons": { | |
"ember-concurrency": "0.7.19", | |
"ember-i18n": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment