5.0.0-beta.1 · Published beta · b06750c5. Published on npm. Match your installed version.
Consumer@mnjs/utils#
The small helpers behind Marionette, available for your own components.
Marionette and @mnjs/data import these same implementations.
npm install @mnjs/utils@5.0.0-beta.1
Use the same version for all Marionette packages. Core and data install utils automatically as a regular dependency. Add it directly when your application imports it.
Building a component#
Methods such as getOption, normalizeMethods, and triggerMethod use their
receiver as the component. Mix them into a prototype or call them with .call().
import { Events, getOption, normalizeMethods, triggerMethod } from '@mnjs/utils';
const component = {
...Events,
getOption,
normalizeMethods,
triggerMethod,
options: { label: 'Inbox' },
onOpen() {
return this.getOption('label');
}
};
component.triggerMethod('open'); // 'Inbox'
component.normalizeMethods({ open: 'onOpen' });
Events#
Events is the shared event implementation used by Marionette, Radio, and native
data. Mix it into an object with Object.assign({}, Events) to use on, off,
trigger, listenTo, and stopListening without core.
Helpers#
Use object spread or Object.assign for ordinary copying and composition.
Inherited enumerable parent statics are copied only inside extend.
getValue(object, key, fallback)reads a value and calls it on the object if it is a function.getOptionreads fromthis.options, then the receiver.mergeOptions(options, keys)copies selected options onto the receiver.normalizeMethods(map)resolves method names on the receiver.resolveMethod(context, method, name)resolves one handler.bindEventsandunbindEventsuse the receiver's listening methods.bindRequestsandunbindRequestsregister or remove channel replies with the receiver as their context.normalizeBindings(context, map)resolves an event map without subscribing.triggerMethod(eventName, ...args)invokes the matchingonEventNamemethod and triggers the event.extendis the function-constructor inheritance helper used by Marionette.MarionetteErroris the same error constructor exported by Marionette.isStringrecognizes primitive and boxed strings.setPropertyassigns a property, treating__proto__as an own data property.
ES modules, CommonJS, and TypeScript declarations are included. The package has no runtime dependencies and declares no side effects. Bundlers can retain only the imported helpers. Marionette's standalone UMD bundles include these helpers; module consumers share the installed package.
Event-building helpers buildEventArgs, eventSplitter, callHandler, and
onceWrap, plus uniqueId, are shared by core and Radio.