Skip to content

Instantly share code, notes, and snippets.

@kevlarr
Last active May 12, 2017 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevlarr/04f934f126d31aa5c4a3e8831c01c0ad to your computer and use it in GitHub Desktop.
Save kevlarr/04f934f126d31aa5c4a3e8831c01c0ad to your computer and use it in GitHub Desktop.
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