Skip to content

Commit

Permalink
Add preference computed property
Browse files Browse the repository at this point in the history
  • Loading branch information
san650 committed Mar 10, 2016
1 parent 38bea7c commit 4ea04fc
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
2. Inject the service on all routes, controllers and components
* create preferences mixin
* create initializer and inject mixin to Route, Controller and Component
3. Computed property
* read from preferences
* read default value
* write to preferences
* return value from `set` operation to update de cp
25 changes: 25 additions & 0 deletions app/utils/preference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Ember from 'ember';

export default function preference(dependentKey, options = {}) {
var key = `preferences.${dependentKey}`;

return Ember.computed(key, {
get() {
var value = this.get(key);

if (typeof value === 'undefined' || value === null) {
if (typeof options.defaultValue === 'function') {
return options.defaultValue();
} else {
return options.defaultValue;
}
}

return value;
},

set(_, value) {
return this.set(key, value);
}
});
}
89 changes: 89 additions & 0 deletions tests/unit/utils/preference-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Ember from 'ember';
import preference from 'ember-preferences/utils/preference';
import { module, test } from 'qunit';

module('Unit | Utility | preference');

function createInstance(options = {}) {
let Class = Ember.Object.extend({
foo: preference('foo', options.cpOptions)
});

let preferences = options.preferences || { foo: 'bar' };

let instance = Class.create({ preferences });

return instance;
}

test('returns values from "preference" property', function(assert) {
let instance = createInstance();

assert.equal(instance.get('foo'), 'bar');
});

test('updates value when the preference changes', function(assert) {
let instance = createInstance();

assert.equal(instance.get('foo'), 'bar');

instance.set('preferences.foo', 'baz');

assert.equal(instance.get('foo'), 'baz');
});

test('returns default value when preference is null', function(assert) {
let instance = createInstance({
cpOptions: {
defaultValue: 'qux'
},

preferences: { foo: null }
});

assert.equal(instance.get('foo'), 'qux');
});

test('returns default value when preference is undefined', function(assert) {
let instance = createInstance({
cpOptions: {
defaultValue: 'qux'
},

preferences: { }
});

assert.equal(instance.get('foo'), 'qux');
});

test('evaluates default value when it is a function', function(assert) {
let instance = createInstance({
cpOptions: {
defaultValue() {
return 'qux';
}
},

preferences: { }
});

assert.equal(instance.get('foo'), 'qux');
});

test('writes configuration to preference object', function(assert) {
let instance = createInstance();

instance.set('foo', 'baz');

assert.equal(instance.get('preferences.foo'), 'baz');
});

test('updates value when the property changes', function(assert) {
let instance = createInstance();

assert.equal(instance.get('foo'), 'bar');

instance.set('foo', 'baz');

assert.equal(instance.get('foo'), 'baz');
});

0 comments on commit 4ea04fc

Please sign in to comment.