Permalink
Browse files

Add getWithDefault

  • Loading branch information...
wycats committed Feb 22, 2012
1 parent 0f1fad8 commit 1d964782c0620ce52c7cd574c5cf1b4a221384dc
@@ -256,6 +256,13 @@ Ember.normalizeTuple = function(target, path) {

Ember.normalizeTuple.primitive = normalizeTuple;

Ember.getWithDefault = function(root, key, defaultValue) {
var value = Ember.get(root, key);

if (value === undefined) { return defaultValue; }
return value;
};

Ember.getPath = function(root, path, _checkGlobal) {
var pathOnly, hasThis, hasStar, isGlobal, ret;

@@ -76,3 +76,82 @@ test('(regression) watched properties on unmodified inherited objects should sti
equal(Ember.get(theRealObject, 'someProperty'), 'foo', 'should return the set value, not false');
});

module("Ember.getWithDefault");

test('should get arbitrary properties on an object', function() {
var obj = {
string: 'string',
number: 23,
boolTrue: true,
boolFalse: false,
nullValue: null
};

for(var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
equal(Ember.getWithDefault(obj, key, "fail"), obj[key], key);
}

obj = {
undef: undefined
}

equal(Ember.getWithDefault(obj, "undef", "default"), "default", "explicit undefined retrieves the default");
equal(Ember.getWithDefault(obj, "not-present", "default"), "default", "non-present key retrieves the default");
});

test('should call unknownProperty if defined and value is undefined', function() {

var obj = {
count: 0,
unknownProperty: function(key) {
equal(key, 'foo', 'should pass key');
this.count++;
return 'FOO';
}
};

equal(Ember.get(obj, 'foo'), 'FOO', 'should return value from unknown');
equal(obj.count, 1, 'should have invoked');
});

testBoth("if unknownProperty is present, it is called", function(get, set) {
var obj = {
count: 0,
unknownProperty: function(key) {
if (key == "foo") {
equal(key, 'foo', "should pass key");
this.count++;
return "FOO";
}
}
};

var count = 0;
Ember.addObserver(obj, 'foo', function() {
count++;
});

equal(Ember.getWithDefault(obj, 'foo', "fail"), 'FOO', 'should return value from unknownProperty');
equal(Ember.getWithDefault(obj, 'bar', "default"), 'default', 'should convert undefined from unknownProperty into default');
});

// ..........................................................
// BUGS
//

test('(regression) watched properties on unmodified inherited objects should still return their original value', function() {

var MyMixin = Ember.Mixin.create({
someProperty: 'foo',
propertyDidChange: Ember.observer(function() {
// NOTHING TO DO
}, 'someProperty')
});

var baseObject = MyMixin.apply({});
var theRealObject = Ember.create(baseObject);

equal(Ember.getWithDefault(theRealObject, 'someProperty', "fail"), 'foo', 'should return the set value, not false');
});

@@ -326,6 +326,10 @@ Ember.Observable = Ember.Mixin.create(/** @scope Ember.Observable.prototype */ {
return this;
},

getWithDefault: function(key, defaultValue) {
return Ember.getWithDefault(this, key, defaultValue);
},

incrementProperty: function(keyName, increment) {
if (!increment) { increment = 1; }
set(this, keyName, (get(this, keyName) || 0)+increment);
@@ -16,3 +16,33 @@ testBoth = function(testname, callback) {
});
};

testWithDefault = function(testname, callback) {
function get(x,y) { return x.get(y); }
function emberget(x,y) { return Ember.get(x,y); }
function embergetwithdefault(x,y,z) { return Ember.getWithDefault(x,y,z); }
function getwithdefault(x,y,z) { return x.getWithDefault(y,z); }
function emberset(x,y,z) { return Ember.set(x,y,z); }
function aget(x,y) { return x[y]; }
function aset(x,y,z) { return (x[y] = z); }

test(testname+' using obj.get()', function() {
callback(emberget, emberset);
});

test(testname+' using obj.getWithDefault()', function() {
callback(getwithdefault, emberset);
});

test(testname+' using Ember.get()', function() {
callback(emberget, emberset);
});

test(testname+' using Ember.getWithDefault()', function() {
callback(embergetwithdefault, emberset);
});

test(testname+' using accessors', function() {
if (Ember.USES_ACCESSORS) callback(aget, aset);
else ok('SKIPPING ACCESSORS');
});
};
@@ -9,7 +9,7 @@ require('ember-runtime/~tests/props_helper');

module('Ember.Object computed property');

testBoth('computed property on instance', function(get, set) {
testWithDefault('computed property on instance', function(get, set) {

var MyClass = Ember.Object.extend({
foo: Ember.computed(function() { return 'FOO'; }).cacheable()
@@ -20,7 +20,7 @@ testBoth('computed property on instance', function(get, set) {
});


testBoth('computed property on subclass', function(get, set) {
testWithDefault('computed property on subclass', function(get, set) {

var MyClass = Ember.Object.extend({
foo: Ember.computed(function() { return 'FOO'; }).cacheable()
@@ -35,7 +35,7 @@ testBoth('computed property on subclass', function(get, set) {
});


testBoth('replacing computed property with regular val', function(get, set) {
testWithDefault('replacing computed property with regular val', function(get, set) {

var MyClass = Ember.Object.extend({
foo: Ember.computed(function() { return 'FOO'; }).cacheable()
@@ -49,7 +49,7 @@ testBoth('replacing computed property with regular val', function(get, set) {

});

testBoth('complex depndent keys', function(get, set) {
testWithDefault('complex depndent keys', function(get, set) {

var MyClass = Ember.Object.extend({

@@ -62,7 +62,7 @@ testBoth('complex depndent keys', function(get, set) {

foo: Ember.computed(function() {
set(this, 'count', get(this, 'count')+1);
return get(get(this, 'bar'), 'baz') + ' ' + get(this, 'count');
return Ember.get(get(this, 'bar'), 'baz') + ' ' + get(this, 'count');
}).property('bar.baz').cacheable()

});
@@ -88,7 +88,7 @@ testBoth('complex depndent keys', function(get, set) {
equal(get(obj2, 'foo'), 'BOOM 22');
});

testBoth('complex depndent keys changing complex dependent keys', function(get, set) {
testWithDefault('complex depndent keys changing complex dependent keys', function(get, set) {

var MyClass = Ember.Object.extend({

@@ -101,7 +101,7 @@ testBoth('complex depndent keys changing complex dependent keys', function(get,

foo: Ember.computed(function() {
set(this, 'count', get(this, 'count')+1);
return get(get(this, 'bar'), 'baz') + ' ' + get(this, 'count');
return Ember.get(get(this, 'bar'), 'baz') + ' ' + get(this, 'count');
}).property('bar.baz').cacheable()

});
@@ -117,7 +117,7 @@ testBoth('complex depndent keys changing complex dependent keys', function(get,

foo: Ember.computed(function() {
set(this, 'count', get(this, 'count')+1);
return get(get(this, 'bar2'), 'baz') + ' ' + get(this, 'count');
return Ember.get(get(this, 'bar2'), 'baz') + ' ' + get(this, 'count');
}).property('bar2.baz').cacheable()
});

@@ -132,7 +132,9 @@ testBoth('complex depndent keys changing complex dependent keys', function(get,
equal(get(obj2, 'foo'), 'BLARG 2', 'should invalidate property');
});

testBoth("can retrieve metadata for a computed property", function(get, set) {
test("can retrieve metadata for a computed property", function() {
var get = Ember.get;

var MyClass = Ember.Object.extend({
computedProperty: Ember.computed(function() {
}).property().meta({ key: 'keyValue' })

1 comment on commit 1d96478

@dmathieu

This comment has been minimized.

Copy link
Contributor

dmathieu commented on 1d96478 Feb 22, 2012

❤️

Please sign in to comment.