mobx-decorated-models
Advanced tools
Comparing version
@@ -283,5 +283,15 @@ (function (global, factory) { | ||
const ary = mobx.observable.array([]); | ||
if (options && options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
if (options) { | ||
if (options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
if (options.extend) { | ||
if ('function' === typeof options.extend) { | ||
options.extend(ary); | ||
} else { | ||
_Object$assign(ary, options.extend); | ||
} | ||
} | ||
} | ||
return ary; | ||
@@ -306,8 +316,9 @@ } | ||
function onHasManySet(change, { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp | ||
}) { | ||
function onHasManySet(change, options) { | ||
if (change.type !== 'update' || !change.newValue) { | ||
return change; | ||
} | ||
const { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp | ||
} = options; | ||
const array = mobx.observable.array(change.newValue); | ||
@@ -374,3 +385,3 @@ for (let i = 0; i < array.length; i += 1) { | ||
const definition = mobx.observable(target, property, descriptor); | ||
if (type === 'belongsTo' && !options.model) { | ||
if ('belongsTo' === type && !options.model) { | ||
options.model = property; | ||
@@ -377,0 +388,0 @@ } |
@@ -279,5 +279,15 @@ import _Map from 'babel-runtime/core-js/map'; | ||
const ary = observable.array([]); | ||
if (options && options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
if (options) { | ||
if (options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
if (options.extend) { | ||
if ('function' === typeof options.extend) { | ||
options.extend(ary); | ||
} else { | ||
_Object$assign(ary, options.extend); | ||
} | ||
} | ||
} | ||
return ary; | ||
@@ -302,8 +312,9 @@ } | ||
function onHasManySet(change, { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp | ||
}) { | ||
function onHasManySet(change, options) { | ||
if (change.type !== 'update' || !change.newValue) { | ||
return change; | ||
} | ||
const { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp | ||
} = options; | ||
const array = observable.array(change.newValue); | ||
@@ -370,3 +381,3 @@ for (let i = 0; i < array.length; i += 1) { | ||
const definition = observable(target, property, descriptor); | ||
if (type === 'belongsTo' && !options.model) { | ||
if ('belongsTo' === type && !options.model) { | ||
options.model = property; | ||
@@ -373,0 +384,0 @@ } |
@@ -45,6 +45,16 @@ import { observable } from 'mobx'; | ||
const ary = observable.array([]); | ||
if (options && options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
if (options) { | ||
if (options.model) { | ||
ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
if (options.extend) { | ||
if ('function' === typeof options.extend){ | ||
options.extend(ary); | ||
} else { | ||
Object.assign(ary, options.extend); | ||
} | ||
} | ||
} | ||
return ary; | ||
} |
@@ -23,6 +23,7 @@ import { observable, intercept } from 'mobx'; | ||
function onHasManySet(change, { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp, | ||
}) { | ||
function onHasManySet(change, options) { | ||
if (change.type !== 'update' || !change.newValue) { return change; } | ||
const { | ||
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp, | ||
} = options; | ||
const array = observable.array(change.newValue); | ||
@@ -86,3 +87,3 @@ for (let i = 0; i < array.length; i += 1) { | ||
const definition = observable(target, property, descriptor); | ||
if (type === 'belongsTo' && !options.model) { | ||
if ('belongsTo' === type && !options.model) { | ||
options.model = property; | ||
@@ -89,0 +90,0 @@ } |
{ | ||
"name": "mobx-decorated-models", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Decorators to make using Mobx for model type structures easier", | ||
@@ -5,0 +5,0 @@ "main": "dist/build.full.js", |
@@ -89,3 +89,3 @@ # Decorators for creating model type structures with mobx | ||
The same logic that is used for belongsTo can also build a stand-alone collection. Collections built this way are instances of mobx `observable.array` with an interceptor that converts assigment into model creation. | ||
The same logic that is used for hasMany can also build a stand-alone collection. Collections built this way are instances of mobx `observable.array` with an interceptor that converts assigment into model creation. | ||
@@ -248,2 +248,4 @@ A collecton can be created like so: | ||
An `extend` property can be provided. If `extend` is a function it will called with the collection whenever one is created. If `extend` is an object, it's properties will be copied onto the collection. | ||
Like `belongsTo`, `hasMany` also converts object assignment to a model | ||
@@ -254,6 +256,13 @@ | ||
class Tire { | ||
@session numberInSet; | ||
@session radius; | ||
@belongsTo vehicle; // will be autoset by the `inverseOf: auto` on Car | ||
} | ||
// will be mixed into Car's tires assocation, so one could call: car.tires.areEqualSize() | ||
const TireHelpers = { | ||
areEqualSize() { | ||
return this.every(t => t.radius === this[0]); | ||
} | ||
}; | ||
@identifiedBy('car') | ||
@@ -263,3 +272,3 @@ class Car { | ||
@session color; | ||
@hasMany({ model: 'Tire', inverseOf: 'vehicle', defaults: {numberInSet: 4} }) tires; | ||
@hasMany({ model: 'Tire', inverseOf: 'vehicle', defaults: {radius: 17}, extend: TireHelpers }) tires; | ||
} | ||
@@ -266,0 +275,0 @@ |
@@ -16,11 +16,3 @@ import { findModel } from '../lib/model-lookup'; | ||
expect(container.boxes).toHaveLength(1); | ||
expect(container.serialize()).toEqual({ | ||
id: 42, | ||
location: 'mid-ship', | ||
name: 'TV1', | ||
tags: [], | ||
boxes: [ | ||
{ container: undefined, depth: 12, height: 8, id: 1, metadata: {}, width: 8 }, | ||
], | ||
}); | ||
expect(container.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -78,10 +70,3 @@ | ||
box.update(attrs); | ||
expect(box.serialize()).toEqual({ | ||
container: { boxes: [], id: 1, location: 'Building #1', name: '#12', tags: [] }, | ||
depth: 12, | ||
height: 4, | ||
id: 2, | ||
metadata: {}, | ||
width: 3, | ||
}); | ||
expect(box.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -94,16 +79,3 @@ | ||
expect(box.container).toBeInstanceOf(Container); | ||
expect(box.serialize()).toEqual({ | ||
container: { | ||
id: 1, | ||
boxes: [], | ||
tags: [], | ||
location: 'Building #1', | ||
name: '#12', | ||
}, | ||
depth: 12, | ||
height: 4, | ||
id: 32, | ||
metadata: {}, | ||
width: 3, | ||
}); | ||
expect(box.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -136,12 +108,3 @@ | ||
expect(container.boxes[0].container).toBe(container); | ||
expect(container.serialize()).toEqual({ | ||
id: 1, | ||
location: 'z1', | ||
name: 'C23', | ||
tags: [], | ||
boxes: [ | ||
{ depth: 1, height: 1, metadata: {}, width: 1 }, | ||
{ depth: 1, height: 1, metadata: {}, width: 4 }, | ||
], | ||
}); | ||
expect(container.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -148,0 +111,0 @@ |
@@ -50,2 +50,10 @@ import { autorun } from 'mobx'; | ||
it('can extend hasMany', () => { | ||
const container = Container.deserialize({ color: 'blue' }); | ||
container.boxes.push({ id: 42 }); | ||
expect(container.boxes.identifiers()).toEqual([42]); | ||
container.boxes[0].sides.push({}); | ||
expect(container.boxes[0].sides.volume).toEqual(2); | ||
}); | ||
it('finds model for belongsTo', () => { | ||
@@ -61,8 +69,3 @@ const box = Box.deserialize({ id: 1, watercraft: { name: 'Boaty' } }); | ||
expect(isSerializable(ship.box, 'vessel')).toBe(false); | ||
expect(ship.serialize()).toEqual({ | ||
name: 'HMS Mobx', | ||
embarks: null, | ||
registration: '', | ||
box: { depth: 1, height: 1, metadata: {}, width: 42 }, | ||
}); | ||
expect(ship.serialize()).toMatchSnapshot(); | ||
expect(ship.box.vessel).toBe(ship); | ||
@@ -75,4 +78,4 @@ expect(ship.box.vessel_association_name).toEqual('box'); | ||
expect(box.color).toEqual('red'); | ||
// no color | ||
expect(box.serialize()).toEqual({ depth: 1, height: 1, metadata: {}, width: 3 }); | ||
expect(box.serialize()).not.toHaveProperty('color'); | ||
expect(box.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -99,10 +102,3 @@ | ||
expect(box.metadata).toEqual({ barcode: 'Z12', color: 'black' }); | ||
expect(box.serialize()).toEqual({ | ||
container: undefined, | ||
depth: 1, | ||
height: 1, | ||
id: undefined, | ||
width: 3, | ||
metadata: { barcode: 'Z12', color: 'black' }, | ||
}); | ||
expect(box.serialize()).toMatchSnapshot(); | ||
}); | ||
@@ -109,0 +105,0 @@ |
@@ -40,2 +40,11 @@ import { observable, computed } from 'mobx'; | ||
@identifiedBy('dimension') | ||
export class Dimension { | ||
constructor(attrs) { | ||
Object.assign(this, attrs); | ||
} | ||
} | ||
@identifiedBy('box') | ||
@@ -60,4 +69,20 @@ export class Box extends RectangularCuboid { | ||
@belongsTo container; | ||
@hasMany({ | ||
model: Dimension, | ||
extend: (array) => { | ||
Object.defineProperty(array, 'volume', { | ||
get() { return this.length * 2; }, | ||
}); | ||
}, | ||
}) sides; | ||
} | ||
const BoxExtensions = { | ||
identifiers() { | ||
return this.map(b => b.id); | ||
}, | ||
}; | ||
@identifiedBy('container') | ||
@@ -83,2 +108,3 @@ export class Container extends RectangularCuboid { | ||
}, | ||
extend: BoxExtensions, | ||
}) boxes; | ||
@@ -85,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35
2.94%338
2.74%221639
-42.85%1635
-75.26%