Skip to content

Instantly share code, notes, and snippets.

@chvonrohr
Last active August 7, 2018 15:01
Show Gist options
  • Save chvonrohr/4f3f73f97414b2452c9179396c3b8710 to your computer and use it in GitHub Desktop.
Save chvonrohr/4f3f73f97414b2452c9179396c3b8710 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import {inject as controller} from '@ember/controller';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
myvar: '',
});
import Ember from 'ember';
import {inject as controller} from '@ember/controller';
import {alias } from '@ember/object/computed';
export default Ember.Controller.extend({
application: controller(),
queryParams: ['myvar'],
myvar: ''
});
import Ember from 'ember';
import {inject as controller} from '@ember/controller';
import {alias } from '@ember/object/computed';
export default Ember.Controller.extend({
application: controller(),
queryParams: ['myvar'],
myvar: ''
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('one');
this.route('two');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
userService: Ember.inject.service(),
firstTime: true,
queryParams: {
myvar: { refreshModel: true },
},
model({ search }) {
if (this.get('firstTime')) {
this.set('firstTime', false);
let myvar = this.get('userSettings.myvar');
}
// model logic here
},
setupController(controller) {
if (this.get('userSettings.myvar') && !controller.get('myvar')) {
controller.set('myvar', this.get('userSettings.myvar'));
}
},
resetController(controller) {
this.get('userSettings').set('myvar', controller.get('myvar'));
this.set('firstTime', true);
}
});
import Ember from 'ember';
export default Ember.Route.extend({
userService: Ember.inject.service(),
firstTime: true,
queryParams: {
myvar: { refreshModel: true },
},
model({ search }) {
if (this.get('firstTime')) {
this.set('firstTime', false);
let myvar = this.get('userSettings.myvar');
}
// model logic here
},
setupController(controller) {
if (this.get('userSettings.myvar') && !controller.get('myvar')) {
controller.set('myvar', this.get('userSettings.myvar'));
}
},
resetController(controller) {
this.get('userSettings').set('myvar', controller.get('myvar'));
this.set('firstTime', true);
}
});
import Ember from 'ember';
export default Ember.Service.extend({
myvar: 'startval'
});
<h1>share query params over routes</h1>
<em>Two routes (one/two) that each have a myvar query-param, that are alias on the parent application-route</em>
{{#link-to 'one'}}one{{/link-to}} | {{#link-to 'two'}}two{{/link-to}}
<br><br>
{{outlet}}
<br>
<br>
<ol>
<li>Go to "{{#link-to 'one'}}one{{/link-to}}"</li>
<li>enter any value in the textfield</li>
<li>Go to "{{#link-to 'two'}}two{{/link-to}}"</li>
<li>!!! value in textfield should remain !!! -> new value initiated and overriden</li>
<li>enter any value in the textfield and back to "{{#link-to 'one'}}one{{/link-to}}"</li>
<li>once sub-controllers are initiated, switching and syncing works</li>
</ol>
ONE: {{input value=myvar}}
TWO: {{input value=myvar}}
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment