mobx-decorated-models
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -70,3 +70,3 @@ (function (global, factory) { | ||
if (model) { | ||
serializr.getDefaultModelSchema(parentModel).props[propName] = cb(model, options); | ||
serializr.getDefaultModelSchema(parentModel).props[propName] = cb(model, options, propName); | ||
} else { | ||
@@ -125,3 +125,3 @@ PendingLookups.push({ parentModel: parentModel, propName: propName, options: options, cb: cb }); | ||
}, | ||
belongsTo: function belongsTo(modelKlass, options) { | ||
belongsTo: function belongsTo(modelKlass, options, propName) { | ||
var defaultSerializer = serializr.object(serializr.getDefaultModelSchema(modelKlass)); | ||
@@ -133,2 +133,3 @@ return { | ||
markNonserializable(model, options.inverseOf); | ||
model[options.inverseOf + '_association_name'] = propName; | ||
model[options.inverseOf] = context.target; | ||
@@ -206,3 +207,3 @@ } | ||
var parentModelSchema = serializr.getDefaultModelSchema(parentModel); | ||
parentModelSchema.props[propName] = cb(referencedModel, options); | ||
parentModelSchema.props[propName] = cb(referencedModel, options, propName); | ||
PendingLookups.splice(i, 1); | ||
@@ -283,3 +284,11 @@ } | ||
var setupModel = mobx.action(function (attrs, modelClass, array, defaultAttributes, inverseOf, parentModel) { | ||
var setupModel = mobx.action(function (_ref) { | ||
var attrs = _ref.attrs, | ||
Klass = _ref.modelClass, | ||
array = _ref.array, | ||
defaultAttributes = _ref.defaultAttributes, | ||
inverseOf = _ref.inverseOf, | ||
parentModel = _ref.parentModel, | ||
parentModelProp = _ref.parentModelProp; | ||
if (defaultAttributes) { | ||
@@ -296,5 +305,6 @@ if (typeof defaultAttributes === 'function') { | ||
if (inverseOf) { | ||
attrs[inverseOf + '_association_name'] = parentModelProp; | ||
attrs[inverseOf] = parentModel; | ||
} | ||
var model = modelClass && !(attrs instanceof modelClass) ? new modelClass(attrs) : attrs; | ||
var model = Klass && !(attrs instanceof Klass) ? new Klass(attrs) : attrs; | ||
markNonserializable(model, inverseOf); | ||
@@ -304,7 +314,7 @@ return model; | ||
function buildInterceptor(_ref, parentModel) { | ||
var modelClass = _ref.modelClass, | ||
className = _ref.className, | ||
defaultAttributes = _ref.defaults, | ||
inverseOf = _ref.inverseOf; | ||
function buildInterceptor(_ref2, parentModel, parentModelProp) { | ||
var modelClass = _ref2.modelClass, | ||
className = _ref2.className, | ||
defaultAttributes = _ref2.defaults, | ||
inverseOf = _ref2.inverseOf; | ||
@@ -320,6 +330,10 @@ return function (change) { | ||
for (var i = 0; i < change.added.length; i += 1) { | ||
change.added[i] = setupModel(change.added[i], modelClass, change.object, defaultAttributes, inverseOf, parentModel); | ||
change.added[i] = setupModel({ | ||
attrs: change.added[i], array: change.object, modelClass: modelClass, | ||
defaultAttributes: defaultAttributes, inverseOf: inverseOf, parentModel: parentModel, parentModelProp: parentModelProp }); | ||
} | ||
} else if (change.type === 'update') { | ||
change.newValue = setupModel(change.newValue, modelClass, change.object, defaultAttributes, inverseOf, parentModel); | ||
change.newValue = setupModel({ | ||
attrs: change.newValue, array: change.object, modelClass: modelClass, | ||
defaultAttributes: defaultAttributes, inverseOf: inverseOf, parentModel: parentModel, parentModelProp: parentModelProp }); | ||
} | ||
@@ -330,6 +344,6 @@ return change; | ||
function buildCollection(options, parentModel) { | ||
function buildCollection(options, parentModel, parentModelProp) { | ||
var ary = mobx.observable.array([]); | ||
if (options.className || options.modelClass) { | ||
ary.intercept(buildInterceptor(options, parentModel)); | ||
ary.intercept(buildInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
@@ -347,6 +361,6 @@ return ary; | ||
var TypeInitializers = { | ||
hasMany: Initializers.array | ||
hasMany: buildCollection | ||
}; | ||
function getInitializer(type, options) { | ||
function getInitializer(type, options, propName) { | ||
var fn = TypeInitializers[type] || Initializers[options.type]; | ||
@@ -358,3 +372,3 @@ if (!fn) { | ||
// eslint-disable-line func-names | ||
return fn(options, this); | ||
return fn(options, this, propName); | ||
}; | ||
@@ -367,3 +381,3 @@ } | ||
getModelSchema(target.constructor).set(property, { name: property, type: type, options: options }); | ||
var initializer = getInitializer(type, options); | ||
var initializer = getInitializer(type, options, property); | ||
if (initializer) { | ||
@@ -370,0 +384,0 @@ descriptor.initializer = initializer; |
@@ -67,3 +67,3 @@ import { createModelSchema, deserialize, getDefaultModelSchema, identifier, list, object, primitive, serialize, update } from 'serializr'; | ||
if (model) { | ||
getDefaultModelSchema(parentModel).props[propName] = cb(model, options); | ||
getDefaultModelSchema(parentModel).props[propName] = cb(model, options, propName); | ||
} else { | ||
@@ -122,3 +122,3 @@ PendingLookups.push({ parentModel: parentModel, propName: propName, options: options, cb: cb }); | ||
}, | ||
belongsTo: function belongsTo(modelKlass, options) { | ||
belongsTo: function belongsTo(modelKlass, options, propName) { | ||
var defaultSerializer = object(getDefaultModelSchema(modelKlass)); | ||
@@ -130,2 +130,3 @@ return { | ||
markNonserializable(model, options.inverseOf); | ||
model[options.inverseOf + '_association_name'] = propName; | ||
model[options.inverseOf] = context.target; | ||
@@ -203,3 +204,3 @@ } | ||
var parentModelSchema = getDefaultModelSchema(parentModel); | ||
parentModelSchema.props[propName] = cb(referencedModel, options); | ||
parentModelSchema.props[propName] = cb(referencedModel, options, propName); | ||
PendingLookups.splice(i, 1); | ||
@@ -280,3 +281,11 @@ } | ||
var setupModel = action(function (attrs, modelClass, array, defaultAttributes, inverseOf, parentModel) { | ||
var setupModel = action(function (_ref) { | ||
var attrs = _ref.attrs, | ||
Klass = _ref.modelClass, | ||
array = _ref.array, | ||
defaultAttributes = _ref.defaultAttributes, | ||
inverseOf = _ref.inverseOf, | ||
parentModel = _ref.parentModel, | ||
parentModelProp = _ref.parentModelProp; | ||
if (defaultAttributes) { | ||
@@ -293,5 +302,6 @@ if (typeof defaultAttributes === 'function') { | ||
if (inverseOf) { | ||
attrs[inverseOf + '_association_name'] = parentModelProp; | ||
attrs[inverseOf] = parentModel; | ||
} | ||
var model = modelClass && !(attrs instanceof modelClass) ? new modelClass(attrs) : attrs; | ||
var model = Klass && !(attrs instanceof Klass) ? new Klass(attrs) : attrs; | ||
markNonserializable(model, inverseOf); | ||
@@ -301,7 +311,7 @@ return model; | ||
function buildInterceptor(_ref, parentModel) { | ||
var modelClass = _ref.modelClass, | ||
className = _ref.className, | ||
defaultAttributes = _ref.defaults, | ||
inverseOf = _ref.inverseOf; | ||
function buildInterceptor(_ref2, parentModel, parentModelProp) { | ||
var modelClass = _ref2.modelClass, | ||
className = _ref2.className, | ||
defaultAttributes = _ref2.defaults, | ||
inverseOf = _ref2.inverseOf; | ||
@@ -317,6 +327,10 @@ return function (change) { | ||
for (var i = 0; i < change.added.length; i += 1) { | ||
change.added[i] = setupModel(change.added[i], modelClass, change.object, defaultAttributes, inverseOf, parentModel); | ||
change.added[i] = setupModel({ | ||
attrs: change.added[i], array: change.object, modelClass: modelClass, | ||
defaultAttributes: defaultAttributes, inverseOf: inverseOf, parentModel: parentModel, parentModelProp: parentModelProp }); | ||
} | ||
} else if (change.type === 'update') { | ||
change.newValue = setupModel(change.newValue, modelClass, change.object, defaultAttributes, inverseOf, parentModel); | ||
change.newValue = setupModel({ | ||
attrs: change.newValue, array: change.object, modelClass: modelClass, | ||
defaultAttributes: defaultAttributes, inverseOf: inverseOf, parentModel: parentModel, parentModelProp: parentModelProp }); | ||
} | ||
@@ -327,6 +341,6 @@ return change; | ||
function buildCollection(options, parentModel) { | ||
function buildCollection(options, parentModel, parentModelProp) { | ||
var ary = observable.array([]); | ||
if (options.className || options.modelClass) { | ||
ary.intercept(buildInterceptor(options, parentModel)); | ||
ary.intercept(buildInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
@@ -344,6 +358,6 @@ return ary; | ||
var TypeInitializers = { | ||
hasMany: Initializers.array | ||
hasMany: buildCollection | ||
}; | ||
function getInitializer(type, options) { | ||
function getInitializer(type, options, propName) { | ||
var fn = TypeInitializers[type] || Initializers[options.type]; | ||
@@ -355,3 +369,3 @@ if (!fn) { | ||
// eslint-disable-line func-names | ||
return fn(options, this); | ||
return fn(options, this, propName); | ||
}; | ||
@@ -364,3 +378,3 @@ } | ||
getModelSchema(target.constructor).set(property, { name: property, type: type, options: options }); | ||
var initializer = getInitializer(type, options); | ||
var initializer = getInitializer(type, options, property); | ||
if (initializer) { | ||
@@ -367,0 +381,0 @@ descriptor.initializer = initializer; |
@@ -18,3 +18,3 @@ import { | ||
if (model) { | ||
getDefaultModelSchema(parentModel).props[propName] = cb(model, options); | ||
getDefaultModelSchema(parentModel).props[propName] = cb(model, options, propName); | ||
} else { | ||
@@ -60,3 +60,3 @@ PendingLookups.push({ parentModel, propName, options, cb }); | ||
hasMany: modelRef => list(object(modelRef)), | ||
belongsTo: (modelKlass, options) => { | ||
belongsTo: (modelKlass, options, propName) => { | ||
const defaultSerializer = object(getDefaultModelSchema(modelKlass)); | ||
@@ -68,2 +68,3 @@ return { | ||
markNonserializable(model, options.inverseOf); | ||
model[`${options.inverseOf}_association_name`] = propName; | ||
model[options.inverseOf] = context.target; | ||
@@ -134,3 +135,3 @@ } | ||
const parentModelSchema = getDefaultModelSchema(parentModel); | ||
parentModelSchema.props[propName] = cb(referencedModel, options); | ||
parentModelSchema.props[propName] = cb(referencedModel, options, propName); | ||
PendingLookups.splice(i, 1); | ||
@@ -137,0 +138,0 @@ } |
@@ -9,3 +9,5 @@ import { | ||
import { markNonserializable } from './serializable' | ||
const setupModel = action(function(attrs, modelClass, array, defaultAttributes, inverseOf, parentModel) { | ||
const setupModel = action(function({ | ||
attrs, modelClass: Klass, array, defaultAttributes, inverseOf, parentModel, parentModelProp, | ||
}) { | ||
if (defaultAttributes) { | ||
@@ -22,5 +24,6 @@ if (typeof defaultAttributes === 'function') { | ||
if (inverseOf) { | ||
attrs[`${inverseOf}_association_name`] = parentModelProp; | ||
attrs[inverseOf] = parentModel; | ||
} | ||
const model = (modelClass && !(attrs instanceof modelClass)) ? new modelClass(attrs) : attrs; | ||
const model = (Klass && !(attrs instanceof Klass)) ? new Klass(attrs) : attrs; | ||
markNonserializable(model, inverseOf); | ||
@@ -30,3 +33,6 @@ return model; | ||
function buildInterceptor({ modelClass, className, defaults: defaultAttributes, inverseOf }, parentModel) { | ||
function buildInterceptor( | ||
{ modelClass, className, defaults: defaultAttributes, inverseOf }, | ||
parentModel, parentModelProp) | ||
{ | ||
return (change) => { | ||
@@ -41,8 +47,10 @@ if (!change.newValue) { | ||
for (let i = 0; i < change.added.length; i += 1) { | ||
change.added[i] = setupModel(change.added[i], modelClass, change.object, | ||
defaultAttributes, inverseOf, parentModel); | ||
change.added[i] = setupModel({ | ||
attrs: change.added[i], array: change.object, modelClass, | ||
defaultAttributes, inverseOf, parentModel, parentModelProp }); | ||
} | ||
} else if (change.type === 'update') { | ||
change.newValue = setupModel(change.newValue, modelClass, change.object, | ||
defaultAttributes, inverseOf, parentModel); | ||
change.newValue = setupModel({ | ||
attrs: change.newValue, array: change.object, modelClass, | ||
defaultAttributes, inverseOf, parentModel, parentModelProp }); | ||
} | ||
@@ -54,6 +62,6 @@ return change; | ||
function buildCollection(options, parentModel) { | ||
function buildCollection(options, parentModel, parentModelProp) { | ||
const ary = observable.array([]); | ||
if (options.className || options.modelClass) { | ||
ary.intercept(buildInterceptor(options, parentModel)); | ||
ary.intercept(buildInterceptor(options, parentModel, parentModelProp)); | ||
} | ||
@@ -69,10 +77,10 @@ return ary; | ||
const TypeInitializers = { | ||
hasMany: Initializers.array, | ||
hasMany: buildCollection, | ||
}; | ||
function getInitializer(type, options) { | ||
function getInitializer(type, options, propName) { | ||
const fn = TypeInitializers[type] || Initializers[options.type]; | ||
if (!fn) { return undefined; } | ||
return function () { // eslint-disable-line func-names | ||
return fn(options, this); | ||
return fn(options, this, propName); | ||
}; | ||
@@ -83,3 +91,3 @@ } | ||
getModelSchema(target.constructor).set(property, { name: property, type, options }); | ||
const initializer = getInitializer(type, options); | ||
const initializer = getInitializer(type, options, property); | ||
if (initializer) { | ||
@@ -86,0 +94,0 @@ descriptor.initializer = initializer; |
{ | ||
"name": "mobx-decorated-models", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Decorators to make using Mobx for model type structures easier", | ||
@@ -5,0 +5,0 @@ "main": "dist/build.full.js", |
@@ -46,3 +46,4 @@ import { Box, Container, Ship } from './test-models'; | ||
expect(container.boxes[0]).toBeInstanceOf(Box); | ||
expect(container.boxes[0].container).toEqual(container); | ||
expect(container.boxes[0].container).toBe(container); | ||
expect(container.boxes[0].container_association_name).toEqual('boxes'); | ||
}); | ||
@@ -57,2 +58,3 @@ | ||
}); | ||
expect(ship.box.container_association_name).toEqual('box') | ||
}); | ||
@@ -59,0 +61,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
186858
1220