Skip to content

Instantly share code, notes, and snippets.

@chardskarth
Created December 16, 2017 09:10
Show Gist options
  • Save chardskarth/7069bab061d05d92f0453fdb4235c5c6 to your computer and use it in GitHub Desktop.
Save chardskarth/7069bab061d05d92f0453fdb4235c5c6 to your computer and use it in GitHub Desktop.
Active button in single component
import Ember from 'ember';
function addNavigation(navigationObject, name, icon){
let toPush = navigationObject.create({
name, icon,
})
this.get('navigations').push(toPush);
}
export default Ember.Component.extend({
init(){
let self = this;
this._super(...arguments);
let navigationObject = Ember.Object.extend({
name: "",
icon: "",
flag: 0,
isActive: Ember.computed('flag', function(){
let isActive = this.get('name') === self.get('currentActive');
return isActive ? "active" : "";
})
});
addNavigation.call(this, navigationObject, 'list', 'icon-list2');
addNavigation.call(this, navigationObject, 'notes', 'icon-file-text');
addNavigation.call(this, navigationObject, 'new', 'icon-cancel-circle');
addNavigation.call(this, navigationObject, 'search', 'icon-search');
addNavigation.call(this, navigationObject, 'journal', 'icon-book');
},
currentActive: "list",
navigationActive: Ember.computed(function(name){
return this.get('currentActive') === name ;
}),
navigations: [],
actions: {
click(navigation){
this.set('currentActive', navigation.name);
this.get('navigations').forEach(x => {
x.set('flag', x.get('flag') + 1 );
});
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{button-navigation}}
{{#each navigations as |nav|}}
<button class="p-2 btn btn-outline-dark rounded-0 {{nav.isActive}}" {{action "click" nav}}>
<span>{{nav.name}}</span>
</button>
{{/each}}
{
"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