ampersand-state
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -266,7 +266,3 @@ var _ = require('underscore'); | ||
if (def.required) { | ||
if (!_.isUndefined(def.default)) { | ||
val = def.default; | ||
} else { | ||
val = this._getDefaultForType(type); | ||
} | ||
val = _.result(def, 'default'); | ||
return this.set(attr, val, options); | ||
@@ -294,3 +290,3 @@ } else { | ||
var dataType = this._dataTypes[type]; | ||
return dataType && dataType.default && dataType.default(); | ||
return dataType && dataType.default; | ||
}, | ||
@@ -339,3 +335,3 @@ | ||
val = (raw) ? this._values[item] : this[item]; | ||
if (typeof val === 'undefined') val = def.default; | ||
if (typeof val === 'undefined') val = _.result(def, 'default'); | ||
if (typeof val !== 'undefined') res[item] = val; | ||
@@ -415,4 +411,9 @@ } | ||
if (desc[1] || desc.required) def.required = true; | ||
// set default if defined | ||
def.default = !_.isUndefined(desc[2]) ? desc[2] : desc.default; | ||
if (typeof def.default === 'object') { | ||
throw new TypeError('The default value for ' + name + ' cannot be an object/array, must be a value or a function which returns a value/object/array'); | ||
} | ||
def.allowNull = desc.allowNull ? desc.allowNull : false; | ||
@@ -441,3 +442,3 @@ if (desc.setOnce) def.setOnce = true; | ||
} | ||
return def.default; | ||
return _.result(def, 'default'); | ||
} | ||
@@ -444,0 +445,0 @@ }); |
{ | ||
"name": "ampersand-state", | ||
"description": "An observable, extensible state object with derived watchable properties.", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -101,7 +101,67 @@ var tape = require('tape'); | ||
test('should have default array/object properties', function (t) { | ||
var Bar = State.extend({ | ||
props: { | ||
list: ['array', true], | ||
hash: ['object', true] | ||
} | ||
}); | ||
var bar = new Bar(); | ||
var otherBar = new Bar(); | ||
t.ok(bar.list !== undefined); | ||
t.ok(bar.hash !== undefined); | ||
//Should create unique instances of the defaults | ||
otherBar.list.push('foo'); | ||
otherBar.hash.foo = 'bar'; | ||
t.ok(bar.list.length === 0); | ||
t.ok(bar.hash.foo === undefined); | ||
t.end(); | ||
}); | ||
test('should throw a useful error setting a default value to an array', function (t) { | ||
t.plan(2); | ||
try { | ||
State.extend({ | ||
props: { list: ['array', true, []] } | ||
}); | ||
} catch (err) { | ||
t.ok(err instanceof TypeError); | ||
t.ok(err.message.match(/value for list cannot be an object\/array/)); | ||
} | ||
}); | ||
test('should throw a useful error setting a default value to an object', function (t) { | ||
t.plan(2); | ||
try { | ||
State.extend({ | ||
props: { list: ['array', true, []] } | ||
}); | ||
} catch (err) { | ||
t.ok(err instanceof TypeError); | ||
t.ok(err.message.match(/value for list cannot be an object\/array/)); | ||
} | ||
}); | ||
test('a default should be settable as a function which returns a value', function (t) { | ||
var Foo = State.extend({ | ||
props: { | ||
anObject: ['object', true, function () { return {foo: 'bar'}; }] | ||
} | ||
}); | ||
var foo = new Foo(); | ||
t.deepEqual(foo.anObject, {foo: 'bar'}); | ||
t.end(); | ||
}); | ||
test('should throw an error setting a derived prop', function (t) { | ||
t.plan(1); | ||
var foo = new Foo(); | ||
try { foo.name = 'bob'; } | ||
catch (err) { t.ok(err instanceof TypeError); } | ||
t.end(); | ||
}); | ||
@@ -108,0 +168,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57782
1415