Skip to content

Instantly share code, notes, and snippets.

@mansona
Last active April 27, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mansona/95e75624d819bc6e4f05dd0b27aaf8fb to your computer and use it in GitHub Desktop.
Save mansona/95e75624d819bc6e4f05dd0b27aaf8fb to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
store: Ember.inject.service(),
router: Ember.inject.service(),
actions: {
createInstance() {
let newInstance = this.get('store').createRecord('instance');
this.set('instance', newInstance);
},
save() {
this.get('instance').save().catch(() => {
console.log('this had an error saving because there is no backend for a twiddle app :) it would work if your ember-data was setup')
}).then(() => {
this.get('router').transitionTo('index')
});
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
goTo: function(route) {
this.transitionToRoute(route);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
edit: function(instance) {
this.transitionToRoute('edit', instance);
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('new')
this.route('edit', {path: ':id'})
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.get('store').createRecord('instance');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.peekAll('instance')
}
});
import Ember from 'ember';
export default Ember.Route.extend({
});
<h1>May I ask a question - pilot 2</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<button {{action 'goTo' 'new'}}>New</button>
<h3>Form:</h3>
{{#if instance}}
{{input placeholder="name" value=instance.name}}
{{input placeholder="address" value=instance.address}}
<br>
Name: {{instance.name}} <br>
Address: {{instance.address}}
<br>
<button {{action 'save'}}>Save</button>
{{else}}
No Instance <br>
<button {{action 'createInstance'}}>Create One</button>
{{/if}}
<hr>
<h2>Edit</h2>
{{edit-instance instance=model}}
{{#each model as |instance|}}
Name: {{instance.name}} <br>
Address: {{instance.address}}
<button {{action 'edit' instance}}>Edit</button>
<br>
{{/each}}
<h2>New</h2>
{{edit-instance}}
{
"version": "0.13.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.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment