Skip to content

Instantly share code, notes, and snippets.

@Microfed
Last active January 29, 2016 02:00
Show Gist options
  • Save Microfed/3385c09ae0772a3e6931 to your computer and use it in GitHub Desktop.
Save Microfed/3385c09ae0772a3e6931 to your computer and use it in GitHub Desktop.
vsi
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
<h1>Prototype of something</h1>
{{outlet}}
import Ember from 'ember';
export default Ember.Service.extend({
cachedCommands: {},
fetchCommandsByHostId(id, force) {
return new Ember.RSVP.Promise((resolve, reject) => {
let dataFromServer = { commands: ['first', 'second', 'third']};
let data = force ? dataFromServer: this.get('cachedCommands')[id] || dataFromServer;
data.hostId = id;
resolve(data);
});
},
validateCommandsForHost(id, commands) {
return new Ember.RSVP.Promise((resolve, reject) => {
let evaluation = ['no' + id, 'no', 'no'];
let newCommands = { hostId: id, commands: commands, evaluation: evaluation };
this.get('cachedCommands')[id] = newCommands;
resolve(newCommands);
});
}
});
import Ember from 'ember';
let { computed } = Ember;
export default Ember.Component.extend({
commands: computed('attrs.data.value.commands.[]', function() {
console.log('get commands', this.attrs.data.value.commands);
return this.attrs.data.value.commands.join('\n');
}),
evaluation: computed('attrs.data.evaluation.[]', function() {
let evaluation = this.attrs.data.value.evaluation;
return evaluation ? evaluation.join('\n') : null;
}),
// fix two-way binding in textarea component. TODO: use one-way binding
didReceiveAttrs(){
this._super(...arguments);
this.set('commands', this.attrs.data.value.commands.join('\n'));
},
actions: {
validate() {
this.attrs.onValidate(this.get('commands').split('\n'));
}
}
});
{{yield}}
<div class='commands-container'>
<div class='commands'>
{{textarea value=commands rows=10}}
</div>
<div class='evaluate'>
<button {{action 'validate'}}>Validate</button>
</div>
{{#if evaluation}}
<div class='evaluation'>
{{textarea value=evaluation rows=10 readonly=true}}
</div>
{{/if}}
</div>
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel(){
this.transitionTo('vsi.build.hosts');
}
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function () {
this.route('vsi', function () {
this.route('build', function () {
this.route('hosts', function () {
this.route('host', {path: ':id'}, function () {
this.route('commands');
});
});
});
});
});
export default Router;
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.commands-container {
margin: 10px;
}
.commands, .evaluate, .evaluation {
display: inline-block;
}
.commands {
width: 200px;
}
.evaluate {
min-width: 100px;
vertical-align: middle;
}
.evaluation {
width: 200px;
}
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
commands: Ember.inject.service(),
hostId: Ember.computed.reads('model.hostId'),
actions: {
evaluate(commands) {
let self = this;
return this.get('commands').validateCommandsForHost(this.get('hostId'),commands).then(function(newCommands) {
self.set('model', newCommands);
});
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
commands: Ember.inject.service(),
model() {
let hostId = this.modelFor(this.routeName.replace('.commands', '')).id;
return this.get('commands').fetchCommandsByHostId(hostId);
}
});
{{host-commands data=model onValidate=(action 'evaluate')}}
import Ember from 'ember';
export default Ember.Route.extend({
afterModel() {
this.transitionTo(this.routeName + '.commands');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return [{ id: 0, ip: '1.1.1.1' }, { id: 1, ip: '2.2.2.2' }];
}
});
<h3>Hosts available</h3>
{{#each model as |host|}}
{{#link-to 'vsi.build.hosts.host' host.id}}{{host.ip}}{{/link-to}}
{{/each}}
{{outlet}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment