Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transitioning by model
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Controller.extend({
thing: Ember.computed(function() {
return this.store.createRecord('thing', { id: 1, coolnessLevel: 5 });
}),
actions: {
// Pretend these are getting data from a form and then doing:
// thing.save().then(() => this.transitionToRoute(..))
byId() {
this.transitionToRoute('things.thing', this.get('thing.id'));
},
byModel() {
this.transitionToRoute('things.thing', this.get('thing'));
},
},
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
coolnessLevel: attr('number'),
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('things', function() {
this.route('new');
this.route('thing', { path: ':thingId' });
});
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
this.transitionTo('things.new');
},
});
<h1>Welcome to {{appName}}</h1>
<br>
{{link-to "add a thing" "things.new"}}
<br>
<br>
<br>
{{outlet}}
<br>
<br>
<p style="color: #999">pretend there's a bunch of inputs here</p>
<p style="color: #999">and the buttons below save the "thing"</p>
<button {{action "byId"}}>transition by id</button>
<button {{action "byModel"}}>transition by model</button>
<p>the thing isn't really that cool</p>
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment