Skip to content

Instantly share code, notes, and snippets.

@phcoliveira
Last active October 24, 2015 00:33
Show Gist options
  • Save phcoliveira/ee5e3d01b2877dce89eb to your computer and use it in GitHub Desktop.
Save phcoliveira/ee5e3d01b2877dce89eb to your computer and use it in GitHub Desktop.
Toggle computed property.
import Ember from 'ember';
// Can't use session or local storage inside this Twiddle, but you get the picture.
var _sessionStorage = {},
// Make _localStorage = { jwt: "something" } to see that the problem is not
// about the value "false", but rather the original value returned by the getter.
_localStorage = {};
export default Ember.Controller.extend({
appName:'Ember Twiddle',
jwt: 'some_fancy_jwt',
onInit: Ember.on('init', function() { _sessionStorage.jwt = this.get('jwt'); }),
rememberMe: Ember.computed({
get() {
return Ember.isPresent(_localStorage.jwt);
},
set(key, value) {
console.debug('service:session | rememberMe.set', value);
if (value) {
_localStorage.jwt = _sessionStorage.jwt;
} else {
delete _localStorage.jwt;
}
return !!value;
}
}).volatile(), // Loose .volatile() to make it work.
localStorage: Ember.computed(function() { return _localStorage.jwt || "undefined" }).volatile()
});
<h1>Welcome to {{appName}}</h1>
<label>{{input type="checkbox" checked=rememberMe}} Do you want to be remembered?</label>
<p>Current localStorage.jwt: {{localStorage}}</p>
<br>
<br>
{{outlet}}
<br>
<br>
{
"version": "0.4.13",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment