Skip to content

Instantly share code, notes, and snippets.

@nightire
Last active January 4, 2019 08:05
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 nightire/e50312d8567678bb17daadb765e7590c to your computer and use it in GitHub Desktop.
Save nightire/e50312d8567678bb17daadb765e7590c to your computer and use it in GitHub Desktop.
Change state by route link
import Controller from '@ember/controller';
import { inject } from '@ember/service';
import { set } from '@ember/object';
export default Controller.extend({
state: inject(),
router: inject(),
actions: {
resetMessage(route) {
event.preventDefault();
if (this.router.currentRouteName !== route) {
this.router.transitionTo(route);
}
set(this.state, 'message', this.state.defaultMessage);
}
}
});
import Controller from '@ember/controller';
import { inject } from '@ember/service';
export default Controller.extend({
state: inject(),
});
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('ofc');
});
export default Router;
import Service from '@ember/service';
import { reads } from '@ember/object/computed';
export default Service.extend({
defaultMessage: 'Initial State',
message: reads('defaultMessage'),
});
<span {{action (mut this.state.message) this.state.defaultMessage}}>
{{link-to "Go to OFC 1" "ofc"}}
</span>
|
{{#link-to "ofc" click=(action (mut this.state.message) this.state.defaultMessage)}}
Go to OFC 2
{{~/link-to}}
|
<a href="#" {{action "resetMessage" "ofc"}}>
Go to OFC 3
</a>
<hr/>
{{outlet}}
<button {{action (mut this.state.message) "Has Changed!"}}>
Change State
</button>
<div>
{{this.state.message}}
</div>
{
"version": "0.15.1",
"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.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment