Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
button-with-states
import Ember from 'ember';
const { $, Component, computed, observer, RSVP, run } = Ember;
const STATES = {
activating: {
label: 'Activating',
classNames: 'in-progress disabled',
disabled: true
},
error: {
classNames: 'dangerous disabled',
disabled: true
},
ok: {
classNames: 'primary'
},
validating: {
label: 'Validating',
classNames: 'in-progress disabled',
disabled: true
},
warning: {
label: 'Watch out',
classNames: 'dangerous'
}
};
export default Ember.Component.extend({
id: 1,
buttonState: STATES.ok,
isDisabled: computed.bool('buttonState.disabled'),
buttonClass: computed('buttonState', function() {
return `accent button-with-state__button ${this.get('buttonState.classNames')}`;
}),
buttonText: computed('buttonState', function() {
return this.get('buttonState.label') || 'Do Something';
}),
someExternalRequest(id) {
return new RSVP.Promise(function(resolve, reject) {
$.ajax(`https://swapi.co/api/people/${id}/`, {
success: function(response) {
resolve(response);
}
});
});
},
checkValidation() {
this.set('buttonState', STATES.validating);
return this.someExternalRequest(this.get('id'))
.then((response) => {
this.set('id', this.get('id') + 1);
if (response.status === 'error') {
this.set('buttonState', STATES.error);
} else if (response.name != null) {
console.log(response);
this.set('buttonState', STATES.warning);
} else {
this.set('buttonState', STATES.ok);
}
});
},
init() {
console.log('initialized ...');
this._super(...arguments);
this.checkValidation();
},
update: observer('id', function() {
this.checkValidation();
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Button with States'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{button-with-state}}
<br>
<br>
<span class='button-label'>{{buttonText}}</span>
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.10.7",
"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.10.0",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.0",
"ember-testing": "2.10.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment