mobx-decorated-models
Advanced tools
Comparing version 0.4.6 to 0.5.0
@@ -88,14 +88,21 @@ (function (global, factory) { | ||
var dateSerializer = function dateSerializer() { | ||
return serializr.custom(function (date) { | ||
return date ? date.toJSON() : null; | ||
}, function (json) { | ||
return json ? new Date(json) : null; | ||
}); | ||
}; | ||
var CustomSerializerTypes = { | ||
date: dateSerializer, | ||
object: objectSerializer, | ||
array: serializr.list | ||
}; | ||
function getSerializer(options, defaultSerializer) { | ||
var serializer = void 0; | ||
if (options.serializer) { | ||
return serializr.custom(options.serializer[0], options.serializer[1]); | ||
} else if (options.type === 'object') { | ||
serializer = objectSerializer; | ||
} else if (options.type === 'array') { | ||
serializer = serializr.list; | ||
} else { | ||
serializer = defaultSerializer; | ||
} | ||
var fns = serializer(); | ||
var fns = (CustomSerializerTypes[options.type] || defaultSerializer)(); | ||
if (options.writeOnly) { | ||
@@ -136,3 +143,3 @@ return { | ||
deserializer: function deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, function (err, model) { | ||
var setter = function setter(err, model) { | ||
if (!err && options.inverseOf) { | ||
@@ -143,4 +150,9 @@ markNonserializable(model, options.inverseOf); | ||
} | ||
cb(err, model); | ||
}); | ||
cb(err, value); | ||
}; | ||
if (value instanceof modelKlass) { | ||
setter(null, value); | ||
} else { | ||
defaultSerializer.deserializer(value, setter); | ||
} | ||
}, | ||
@@ -147,0 +159,0 @@ serializer: function serializer(belongsTo, name, parent) { |
@@ -85,14 +85,21 @@ import { createModelSchema, custom, deserialize, getDefaultModelSchema, identifier, list, object, primitive, serialize, update } from 'serializr'; | ||
var dateSerializer = function dateSerializer() { | ||
return custom(function (date) { | ||
return date ? date.toJSON() : null; | ||
}, function (json) { | ||
return json ? new Date(json) : null; | ||
}); | ||
}; | ||
var CustomSerializerTypes = { | ||
date: dateSerializer, | ||
object: objectSerializer, | ||
array: list | ||
}; | ||
function getSerializer(options, defaultSerializer) { | ||
var serializer = void 0; | ||
if (options.serializer) { | ||
return custom(options.serializer[0], options.serializer[1]); | ||
} else if (options.type === 'object') { | ||
serializer = objectSerializer; | ||
} else if (options.type === 'array') { | ||
serializer = list; | ||
} else { | ||
serializer = defaultSerializer; | ||
} | ||
var fns = serializer(); | ||
var fns = (CustomSerializerTypes[options.type] || defaultSerializer)(); | ||
if (options.writeOnly) { | ||
@@ -133,3 +140,3 @@ return { | ||
deserializer: function deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, function (err, model) { | ||
var setter = function setter(err, model) { | ||
if (!err && options.inverseOf) { | ||
@@ -140,4 +147,9 @@ markNonserializable(model, options.inverseOf); | ||
} | ||
cb(err, model); | ||
}); | ||
cb(err, value); | ||
}; | ||
if (value instanceof modelKlass) { | ||
setter(null, value); | ||
} else { | ||
defaultSerializer.deserializer(value, setter); | ||
} | ||
}, | ||
@@ -144,0 +156,0 @@ serializer: function serializer(belongsTo, name, parent) { |
@@ -35,4 +35,15 @@ import { | ||
const dateSerializer = () => customSerializer( | ||
date => (date ? date.toJSON() : null), | ||
json => (json ? new Date(json) : null), | ||
); | ||
const CustomSerializerTypes = { | ||
date: dateSerializer, | ||
object: objectSerializer, | ||
array: list, | ||
}; | ||
function getSerializer(options, defaultSerializer) { | ||
let serializer; | ||
if (options.serializer) { | ||
@@ -42,10 +53,4 @@ return customSerializer(options.serializer[0], | ||
); | ||
} else if (options.type === 'object') { | ||
serializer = objectSerializer; | ||
} else if (options.type === 'array') { | ||
serializer = list; | ||
} else { | ||
serializer = defaultSerializer; | ||
} | ||
const fns = serializer(); | ||
const fns = (CustomSerializerTypes[options.type] || defaultSerializer)(); | ||
if (options.writeOnly) { | ||
@@ -79,3 +84,3 @@ return { | ||
deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, (err, model) => { | ||
const setter = (err, model) => { | ||
if (!err && options.inverseOf) { | ||
@@ -86,4 +91,9 @@ markNonserializable(model, options.inverseOf); | ||
} | ||
cb(err, model); | ||
}); | ||
cb(err, value); | ||
}; | ||
if (value instanceof modelKlass) { | ||
setter(null, value); | ||
} else { | ||
defaultSerializer.deserializer(value, setter); | ||
} | ||
}, | ||
@@ -90,0 +100,0 @@ serializer(belongsTo, name, parent) { |
{ | ||
"name": "mobx-decorated-models", | ||
"version": "0.4.6", | ||
"version": "0.5.0", | ||
"description": "Decorators to make using Mobx for model type structures easier", | ||
@@ -5,0 +5,0 @@ "main": "dist/build.full.js", |
@@ -101,3 +101,3 @@ # Decorators for creating model type structures with mobx | ||
const collection = createCollection({ model: ModelInCollection }); | ||
const collection = createCollection({ model: Foo }); | ||
collection.push({ name: 'bar' }); | ||
@@ -111,3 +111,3 @@ collection[0].myName(); // will return "bar", since it's coerced into an instance of Foo | ||
### identifiedBy('<identifier>') | ||
### identifiedBy('identifier') | ||
@@ -123,4 +123,3 @@ Marks a class as serializable. | ||
However, it's primary purpose is to remember classes for hasMany/belongsTo lookups. See discussion | ||
above regarding `lookupModelUsing` and `rememberModelUsing`. | ||
It’s primary purpose is to remember classes for hasMany/belongsTo lookups. This allows the associations to refer to models without having to load them. Often models will refer to one another, making it difficult for each of them obtain a direct reference. | ||
@@ -135,3 +134,3 @@ ### identifier | ||
The type of field can be set to `array` or `object` by specifying options. | ||
The type of field can be set to `array` or `object`, or `date` by specifying options. | ||
@@ -145,8 +144,11 @@ *example:* | ||
@field({ type: 'array' }) tags; // defaults to [] | ||
@field({ type: 'date' }) occured; // no default value is set | ||
} | ||
const foo = new Foo(); | ||
foo.update({ occured: '2013-10-21T13:28:06.419Z' }); | ||
console.log(foo.occured); // Mon Oct 21 2013 08:28:06 GMT-0500 (CDT), provided you're in CDT :) | ||
foo.tags.push('one'); | ||
foo.options.set('one', 1); | ||
foo.serialize(); // => { tags: ['one'], options: { one: 1 } } | ||
foo.serialize(); // => { tags: ['one'], options: { one: 1 }, occured: '2013-10-21T13:28:06.419Z' } | ||
``` | ||
@@ -153,0 +155,0 @@ |
@@ -20,2 +20,8 @@ import { Container, Box, Ship } from './test-models'; | ||
it('can serialize/deserialize dates', () => { | ||
const ship = Ship.deserialize({ embarks: '2013-10-21T13:28:06.419Z' }); | ||
expect(ship.embarks).toEqual(new Date('2013-10-21T13:28:06.419Z')); | ||
expect(ship.serialize()).toEqual({ embarks: '2013-10-21T13:28:06.419Z' }); | ||
}); | ||
it('can deserialize arrays', () => { | ||
@@ -80,2 +86,8 @@ const json = [ | ||
it('can assign when a model is given for a belongsTo', () => { | ||
const box = new Box(); | ||
const boat = Ship.deserialize({ box }); | ||
expect(boat.box).toBe(box); | ||
}); | ||
it('hasMany', () => { | ||
@@ -82,0 +94,0 @@ const container = new Container({ id: 1, name: 'C23', location: 'z1' }); |
@@ -61,3 +61,3 @@ import { autorun } from 'mobx'; | ||
expect(ship.serialize()).toEqual({ | ||
name: 'HMS Mobx', | ||
name: 'HMS Mobx', embarks: null, | ||
box: { depth: 1, height: 1, metadata: {}, width: 42 }, | ||
@@ -64,0 +64,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { observable, computed } from 'mobx'; | ||
import { computed } from 'mobx'; | ||
import { identifiedBy, field, session, belongsTo, hasMany, identifier } from '../index'; | ||
@@ -21,3 +21,3 @@ | ||
@field({ serializer: shipCargoSerializer }) cargoCount; | ||
@field({ type: 'date' }) embarks; | ||
@belongsTo({ inverseOf: 'vessel' }) box; | ||
@@ -24,0 +24,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
217298
1622
306