Sometimes I encode a single behaviour of my objects into a helper function. This helper function then makes it easier to make type definitions more DRY and composable. Below is an example for adding an id identifier strategy.
Right now we have to wrap the type definition call. It would be nice to have a .mixin method, to allow for mixin in these behaviour generating functions.
// adds an automatic generated unique id to the object
// and adds `shortId` property for displaying in the ui
const withIdProperty = type =>
type
.props({
// add id property with random default
id: t.optional(
t.identifier(t.string),
() => `${type.name}-${generateUUID()}`
),
})
.views(self => ({
get shortId() {
// use this in the app
return self.id.split('-')[1];
},
}));
// current way
const Record = withIdProperty(
t.model('Record', {
name: t.string,
})
);
// proposed way
const Record = t
.model('Record', {
name: t.string,
})
.mixin(withIdProperty);
Where .mixin could be implemented like:
type.mixin = function(behaviour) {
return behaviour(this);
}
Sometimes I encode a single behaviour of my objects into a helper function. This helper function then makes it easier to make type definitions more DRY and composable. Below is an example for adding an
ididentifier strategy.Right now we have to wrap the type definition call. It would be nice to have a
.mixinmethod, to allow for mixin in these behaviour generating functions.Where
.mixincould be implemented like: