mobx-decorated-models
Advanced tools
Comparing version 0.0.7 to 0.1.0
12
index.js
import model from './lib/class-decorator'; | ||
import { lookupModelUsing, rememberModelUsing } from './lib/model-lookup'; | ||
import { field, session, belongsTo, hasMany, identifier } from './lib/property-decorators'; | ||
export { | ||
model, identifier, field, session, belongsTo, hasMany | ||
} | ||
model, | ||
identifier, | ||
field, | ||
session, | ||
belongsTo, | ||
hasMany, | ||
lookupModelUsing, | ||
rememberModelUsing, | ||
}; |
@@ -6,16 +6,8 @@ import { | ||
import { registerModel, findModel } from './model-lookup'; | ||
import getSchema from './schema'; | ||
const ModelsMap = new Map(); | ||
const PendingLookups = []; | ||
function capitalize(str) { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
} | ||
function findModel(name, options) { | ||
const modelName = options.className ? options.className : name; | ||
return ModelsMap[modelName] || ModelsMap[capitalize(modelName)]; | ||
} | ||
function addReference(parentModel, propName, options, cb) { | ||
@@ -30,3 +22,3 @@ const model = findModel(propName, options); | ||
function getSerializer(options, defaultSerializer){ | ||
function getSerializer(options, defaultSerializer) { | ||
let serializer; | ||
@@ -59,5 +51,3 @@ if (options.type === 'object') { | ||
hasMany: modelRef => list(object(modelRef)), | ||
belongsTo: (model, opt) => { | ||
return object(getDefaultModelSchema(model)) | ||
}, | ||
belongsTo: model => object(getDefaultModelSchema(model)), | ||
}; | ||
@@ -92,3 +82,4 @@ | ||
ModelsMap[model.name] = model; | ||
registerModel(model); | ||
const serializeSchema = {}; | ||
@@ -95,0 +86,0 @@ |
@@ -9,3 +9,3 @@ import { | ||
object: () => observable.map({}), | ||
array: () => observable.array([]), | ||
array: () => observable.array([]), | ||
}; | ||
@@ -21,3 +21,2 @@ | ||
function addAttribute(type, target, property, descriptor, options = {}) { | ||
@@ -40,13 +39,10 @@ getModelSchema(target.constructor).set(property, { name: property, type, options }); | ||
const identifier = (...args) => buildAttributeDecorator('identifier', args); | ||
const identifier = (...args) => buildAttributeDecorator('identifier', args); | ||
const field = (...args) => buildAttributeDecorator('field', args); | ||
const session = (...args) => buildAttributeDecorator('session', args); | ||
const belongsTo = (...args) => buildAttributeDecorator('belongsTo', args); | ||
const hasManyBuilder = (type, target, property, descriptor, options = {}) => | ||
addAttribute(type, target, property, descriptor, options); | ||
const hasMany = (...args) => buildAttributeDecorator('hasMany', args, hasManyBuilder); | ||
export { field, session, belongsTo, hasMany, identifier }; |
{ | ||
"name": "mobx-decorated-models", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"description": "Decorators to make using Mobx for model type structures easier", | ||
@@ -5,0 +5,0 @@ "main": "dist/build.full.js", |
@@ -59,6 +59,66 @@ # Decorators for creating model type structures with mobx | ||
### Decorators | ||
### Controlling model lookups | ||
By default, the class `@model` decorator uses the `name` property of each class as a lookup key so | ||
that `hasMany` and `belongsTo` relation ships can be established. | ||
This allows things like the below mappings to still work even though the two files can't easily include each other: | ||
```javascript | ||
// chair.js | ||
import { model, belongsTo } from 'mobx-decorated-models'; | ||
@model | ||
class Chair { | ||
belongsTo 'table' | ||
} | ||
// table.js | ||
import { model, hasMany } from 'mobx-decorated-models'; | ||
@model | ||
class Table { | ||
hasMany({ model: 'Chair' }) 'seats' | ||
} | ||
``` | ||
This works well enough, however using the `name` property is fragile, since it relies on the class name | ||
not changing. Certain JS minimizers may rename classes. | ||
If custom logic is needed, it's possible to supply custom "record" and "lookup" functions. | ||
*Example* that uses a static `identifiedBy` property. | ||
```javascript | ||
import { model, lookupModelUsing, rememberModelUsing } from 'mobx-decorated-models'; | ||
import { capitalize, singularize } from 'utility'; | ||
const Models = {}; | ||
lookupModelUsing((propertyName, propertyOptions) => { | ||
return Models[propertyOptions.className] || | ||
Models[capitalize(propertyName)] || | ||
Models[capitalize(singularize(propertyName))]; | ||
}); | ||
rememberModelUsing(klass => Models[klass.identifiedBy] = klass); | ||
@model | ||
class ATestingModel { | ||
static identifiedBy = 'test'; | ||
belongsTo document; | ||
} | ||
@model | ||
class Document { | ||
static identifiedBy = 'document'; | ||
hasMany({ className: 'test' }) testRuns; | ||
} | ||
``` | ||
### Decorators | ||
#### model | ||
@@ -68,3 +128,3 @@ | ||
It adds a few convenience methods: | ||
It adds a few convenience methods to classes: | ||
@@ -75,2 +135,5 @@ * static `deserialize` method. Used to turn JSON structure into a model (or collection of models) | ||
However, it's primary purpose is to remember classes for hasMany/belongsTo lookups. See discussion | ||
above regarding `lookupModelUsing` and `rememberModelUsing`. | ||
#### identifier | ||
@@ -127,3 +190,2 @@ | ||
* Use a provided lookup function to control the lookup | ||
* Sessions properties that will be set from JSON but won't be serialized. | ||
* Sessions: properties that will be set from JSON but won't be serialized. https://github.com/mobxjs/serializr/pull/32 is needed before this can be supported |
import { Container, Box } from './test-models'; | ||
describe('Class Decorators', () => { | ||
it('adds static deserialize method and serialize to prototype', () => { | ||
@@ -14,3 +13,3 @@ const attrs = { id: 42, name: 'TV1', location: 'mid-ship', tags: [], boxes: [ | ||
boxes: [ | ||
{ container: undefined, depth: 12, height: 8, id: 1, metadata: {}, width: 8}, | ||
{ container: undefined, depth: 12, height: 8, id: 1, metadata: {}, width: 8 }, | ||
], | ||
@@ -17,0 +16,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
154291
24
849
188