mobx-decorated-models
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -42,2 +42,11 @@ (function (global, factory) { | ||
function isSerializable(model, property) { | ||
return !!(!model.$nonSerializable || -1 === model.$nonSerializable.indexOf(property)); | ||
} | ||
function markNonserializable(model, property) { | ||
model.$nonSerializable = model.$nonSerializable || []; | ||
model.$nonSerializable.push(property); | ||
} | ||
var getModelSchema = function (model) { | ||
@@ -116,8 +125,16 @@ if (model.$schema) { | ||
}, | ||
belongsTo: function belongsTo(model) { | ||
var defaultSerializer = serializr.object(serializr.getDefaultModelSchema(model)); | ||
belongsTo: function belongsTo(modelKlass, options) { | ||
var defaultSerializer = serializr.object(serializr.getDefaultModelSchema(modelKlass)); | ||
return { | ||
deserializer: defaultSerializer.deserializer, | ||
deserializer: function deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, function (err, model) { | ||
if (!err && options.inverseOf) { | ||
markNonserializable(model, options.inverseOf); | ||
model[options.inverseOf] = context.target; | ||
} | ||
cb(err, model); | ||
}); | ||
}, | ||
serializer: function serializer(belongsTo, name, parent) { | ||
if (parent.$nonSerializable && -1 !== parent.$nonSerializable.indexOf(name)) { | ||
if (!isSerializable(parent, name)) { | ||
return undefined; | ||
@@ -279,4 +296,3 @@ } | ||
var model = modelClass && !(attrs instanceof modelClass) ? new modelClass(attrs) : attrs; | ||
model.$nonSerializable = model.$nonSerializable || []; | ||
model.$nonSerializable.push(inverseOf); | ||
markNonserializable(model, inverseOf); | ||
return model; | ||
@@ -283,0 +299,0 @@ }); |
@@ -39,2 +39,11 @@ import { createModelSchema, deserialize, getDefaultModelSchema, identifier, list, object, primitive, serialize, update } from 'serializr'; | ||
function isSerializable(model, property) { | ||
return !!(!model.$nonSerializable || -1 === model.$nonSerializable.indexOf(property)); | ||
} | ||
function markNonserializable(model, property) { | ||
model.$nonSerializable = model.$nonSerializable || []; | ||
model.$nonSerializable.push(property); | ||
} | ||
var getModelSchema = function (model) { | ||
@@ -113,8 +122,16 @@ if (model.$schema) { | ||
}, | ||
belongsTo: function belongsTo(model) { | ||
var defaultSerializer = object(getDefaultModelSchema(model)); | ||
belongsTo: function belongsTo(modelKlass, options) { | ||
var defaultSerializer = object(getDefaultModelSchema(modelKlass)); | ||
return { | ||
deserializer: defaultSerializer.deserializer, | ||
deserializer: function deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, function (err, model) { | ||
if (!err && options.inverseOf) { | ||
markNonserializable(model, options.inverseOf); | ||
model[options.inverseOf] = context.target; | ||
} | ||
cb(err, model); | ||
}); | ||
}, | ||
serializer: function serializer(belongsTo, name, parent) { | ||
if (parent.$nonSerializable && -1 !== parent.$nonSerializable.indexOf(name)) { | ||
if (!isSerializable(parent, name)) { | ||
return undefined; | ||
@@ -276,4 +293,3 @@ } | ||
var model = modelClass && !(attrs instanceof modelClass) ? new modelClass(attrs) : attrs; | ||
model.$nonSerializable = model.$nonSerializable || []; | ||
model.$nonSerializable.push(inverseOf); | ||
markNonserializable(model, inverseOf); | ||
return model; | ||
@@ -280,0 +296,0 @@ }); |
@@ -10,3 +10,3 @@ import { | ||
import { registerModel, findModel } from './model-lookup'; | ||
import { isSerializable, markNonserializable } from './serializable' | ||
import getSchema from './schema'; | ||
@@ -60,16 +60,23 @@ | ||
hasMany: modelRef => list(object(modelRef)), | ||
belongsTo: model => { | ||
const defaultSerializer = object(getDefaultModelSchema(model)); | ||
belongsTo: (modelKlass, options) => { | ||
const defaultSerializer = object(getDefaultModelSchema(modelKlass)); | ||
return { | ||
deserializer: defaultSerializer.deserializer, | ||
serializer: (belongsTo, name, parent) => { | ||
if (parent.$nonSerializable && -1 !== parent.$nonSerializable.indexOf(name)){ | ||
return undefined; | ||
} | ||
deserializer(value, cb, context) { | ||
defaultSerializer.deserializer(value, (err, model) => { | ||
if (!err && options.inverseOf) { | ||
markNonserializable(model, options.inverseOf); | ||
model[options.inverseOf] = context.target; | ||
} | ||
cb(err, model); | ||
}); | ||
}, | ||
serializer(belongsTo, name, parent) { | ||
if (!isSerializable(parent, name)) { return undefined; } | ||
return defaultSerializer.serializer(belongsTo); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
const MixedInInstanceMethods = { | ||
@@ -76,0 +83,0 @@ |
@@ -8,3 +8,3 @@ import { | ||
import getModelSchema from './schema'; | ||
import { markNonserializable } from './serializable' | ||
const setupModel = action(function(attrs, modelClass, array, defaultAttributes, inverseOf, parentModel) { | ||
@@ -25,4 +25,3 @@ if (defaultAttributes) { | ||
const model = (modelClass && !(attrs instanceof modelClass)) ? new modelClass(attrs) : attrs; | ||
model.$nonSerializable = model.$nonSerializable || []; | ||
model.$nonSerializable.push(inverseOf); | ||
markNonserializable(model, inverseOf); | ||
return model; | ||
@@ -29,0 +28,0 @@ }); |
{ | ||
"name": "mobx-decorated-models", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Decorators to make using Mobx for model type structures easier", | ||
@@ -5,0 +5,0 @@ "main": "dist/build.full.js", |
@@ -169,11 +169,30 @@ # Decorators for creating model type structures with mobx | ||
*example:* | ||
```javascript | ||
class Foo { | ||
@belongsTo bar; // will look for a `Bar` class | ||
@belongsTo({ className: 'Person' }) name; // looks for class `Person` | ||
class Person({ | ||
@identifier id; | ||
@field name; | ||
@belongsTo({ className: 'Pants', inverseOf: 'owner' }) outfit; | ||
}) | ||
class Pants { | ||
@session color; | ||
@belongsTo({ className: 'Person' }) owner; // looks for class `Person` | ||
} | ||
``` | ||
Can be given a `inverseOf` which will set auto set this property to it's parent when it's deserialized. | ||
For instance the Pants model above will have it's owner property set to the "Ralph" Person model: | ||
```javascript | ||
Person.deserialize({ | ||
id: 1, name: 'Ralph', outfit: { color: 'RED' } | ||
}) | ||
``` | ||
**Note**: When using `inverseOf`, the auto-set property is not serialized in order to prevent circular references. | ||
#### hasMany | ||
@@ -180,0 +199,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { Box, Container } from './test-models'; | ||
import { Box, Container, Ship } from './test-models'; | ||
import { autorun, observable } from 'mobx'; | ||
@@ -42,3 +42,3 @@ import { update } from 'serializr'; | ||
it('sets an inverse', () => { | ||
it('sets an inverse for hasMany', () => { | ||
const container = Container.deserialize({ id: 1, name: 'Bob', location: 'water' }); | ||
@@ -50,7 +50,18 @@ container.boxes.push({}); | ||
xit('merges both attributes and session props', () => { | ||
const box = Box.deserialize({ width: 3, isVisible: true }); | ||
expect(box.isVisible).toEqual(true); | ||
it('sets an inverse for belongsTo', () => { | ||
const ship = Ship.deserialize({ name: 'HMS Mobx', box: { width: 42 } }); | ||
expect(ship.box.container).toBe(ship); | ||
expect(ship.serialize()).toEqual({ | ||
name: 'HMS Mobx', | ||
box: { depth: 1, height: 1, metadata: {}, width: 42 }, | ||
}); | ||
}); | ||
it('merges both attributes and session props', () => { | ||
const box = Box.deserialize({ width: 3, color: 'red' }); | ||
expect(box.color).toEqual('red'); | ||
// no color | ||
expect(box.serialize()).toEqual({ depth: 1, height: 1, metadata: {}, width: 3 }); | ||
}); | ||
it('can observe associations', () => { | ||
@@ -57,0 +68,0 @@ const container = Container.deserialize({ id: 1, name: 'Bob', location: 'water' }); |
@@ -12,2 +12,9 @@ import { observable, computed } from 'mobx'; | ||
@modelDecorator | ||
export class Ship { | ||
@identifier name; | ||
@belongsTo({ inverseOf: 'container' }) box; | ||
} | ||
@modelDecorator | ||
export class Box extends RectangularCuboid { | ||
@@ -14,0 +21,0 @@ @identifier id; |
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
183516
24
1183
239