Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kumkanillam/392e6b107a3b96fbf875f580c19a77bb to your computer and use it in GitHub Desktop.
Save kumkanillam/392e6b107a3b96fbf875f580c19a77bb to your computer and use it in GitHub Desktop.
Recycled Route State
import Ember from 'ember';
export default Ember.Component.extend({
init() {
this._super();
this.instanceState = "I was created at: " + Date.now();
}
});
import Ember from 'ember';
const { get, set } = Ember;
export default Ember.Component.extend({
getContent() {
const obj = get(this, 'model');
if (!obj) { return obj; }
if (typeof obj === 'object' && obj.content) {
return get(obj, 'content');
}
return obj;
},
didUpdateAttrs() {
let newContent = this.getContent();
let isSlotOne = get(this, 'useSlotOne');
if (isSlotOne && newContent !== get(this, 'slotOne')) {
set(this, 'slotTwo', newContent);
set(this, 'useSlotOne', false);
} else if (!isSlotOne && newContent !== get(this, 'slotTwo')) {
set(this, 'slotOne', newContent);
set(this, 'useSlotOne', true);
}
},
init() {
this._super();
this.useSlotOne = true;
this.slotOne = this.getContent();
this.slotTwo = null;
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('my-route', { path: '/:id' });
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
someRouteState: null,
beforeModel() {
const params = this.paramsFor(this.routeName);
const id = params.id;
if (!this.someRouteState || this.someRouteState.routeId !== id) {
this.someRouteState = {
routeId: id
};
}
},
model({ id }) {
return {
id,
name: 'I am model ' + id
};
},
setupController(controller, model) {
this._super(controller, model);
Ember.set(controller, 'state', this.someRouteState);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
{{#link-to 'my-route' '1'}}Go to model 1{{/link-to}}
{{#link-to 'my-route' '2'}}Go to model 2{{/link-to}}
<br>
{{outlet}}
<br>
<br>
<h3>Unbound model.name: {{unbound model.name}}</h3>
<h3>model.name: {{model.name}}</h3>
<h3>Instance (init()) state: {{instanceState}}</h3>
{{#if useSlotOne}}
{{yield slotOne}}
{{else}}
{{yield slotTwo}}
{{/if}}
<h1>I am route {{model.id}}</h1>
<h2>I am some route state: {{state.routeId}}</h2>
{{conditional-state model=model}}
<br><hr><br>
{{#never-same model=model as |model|}}
{{conditional-state model=model}}
{{/never-same}}
{
"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