Skip to content

Instantly share code, notes, and snippets.

@sheriffderek
Last active January 19, 2018 14:00
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 sheriffderek/839c2597d32cfad801c87c358497ddd0 to your computer and use it in GitHub Desktop.
Save sheriffderek/839c2597d32cfad801c87c358497ddd0 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Empty record created example'
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import Ember from 'ember';
export default Model.extend({
firstName: attr('string', {defaultValue: 'Jasper'}),
lastName: attr('string', {defaultValue: 'Johns'}),
fullName: Ember.computed('firstName', 'lastName', function() {
return this.get('firstName') + ' ' + this.get('lastName');
}),
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('orders', { path: '/' });
this.route('other');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.get('store').createRecord('order');
},
});
import Ember from 'ember';
export default Ember.Route.extend({
});
<h1>{{appName}}</h1>
<div><h2>/application</h2></div>
{{link-to 'Orders' 'orders'}}
{{link-to 'Other route' 'other'}}
{{outlet}}
<div><h2>/orders</h2></div>
<p>Every time the 'orders' route is visited, a record is created / and if you leave that page - without saving an order etc - then ember-data ends up with a bunch of meaningless records.</p>
<p>I want to 'create' the record so that I can use the order model and it's computed properties in the form. What are some standard ways people deal with this?</p>
<p>It sounds confusing to check when leaving the route.</p>
<p>You could filter and order list by checking for ids.</p>
{{input value=model.firstName}}
{{input value=model.lastName}}
<p>{{model.fullName}}</p>
<div><h2>/other</h2></div>
<p>Just an example route to switch between</p>
{
"version": "0.13.0",
"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