pwix:field
What is it ?
A package to manage field definitions in Meteor. You define here at once all specifications needed for both:
-
SimpleSchema definition, with
aldeed:simple-schema -
Datatables display, with
aldeed:tabular -
forms input, with
pwix:forms -
getter, setter and both client and server sides checks
-
help lines.
Installation
This Meteor package is installable with the usual command:
meteor add pwix:field
Usage
1 import { Field } from 'meteor/pwix:field'; 2 3 // define your fields specifications, both suitable for schema collection, tabular display and form edition 4 // this is mainly a SimpleSchema extenstion 5 const fieldSet = new Field.Set( 6 { 7 name: 'name' 8 type: String 9 }, 10 { 11 name: 'surname', 12 type: String, 13 optional: true 14 } 15 );
This package is not tied to a client or server side. We strongly suggest to use it in common code.
Provides
Field
The exported Field global object provides following items:
Functions
Field.configure()
See [below](#configuration).
Classes
Field.Def
A class which provides the ad-hoc definitions for (almost) every use of a field in an application, and in particular:
- to a
SimpleSchemacollection schema through theField.ISchemainterface - to a
Datatabletabular display - to the
Forms.Checkerclass provided bypwix:forms - as a help memento with
help_-prefixed keys.
A Field.Def is instanciated with an object with some specific keys, depending of the target usage:
-
Mongo schema
All
SimpleSchemakeys can be set in this field definition, and will be passed to theSimpleSchema()instanciation.Some particular keys are defined and are considered here:
-
nameOptional, the name of the field.
Though this field is optional at the
Field.Deflevel, it is mandatory to trigger a schema definition. In other words,Field.Defdefinitions are just ignored from schema point of view if nonameis set. -
schemaWhen
false, ignore thisField.Defdefinition from schema point of view even if anameis set.
-
-
Tabular display
Tabular display is managed through
Datatable.The
Field.Defdefinitions are used to build thecolumnsdefinition argument atTabular.Tableinstanciation time. All arguments accepted in thiscolumnsdefinition can be provided here, with adt_prefix, plus following keys:-
tabularOptional, whether to have this field in the columns of a tabular display, defaulting to
true.The whole field definition is ignored from tabular point of view when
tabularis false. -
dt_dataOptional, whether to have this field as a data subscription in a tabular display, defaulting to
trueif anameis set.A named field defaults to be subscribed to by a tabular display. This option prevents to have a useless data subscription.
-
dt_template -
dt_templateContextReplace the
tmplandtmplContextdefined byaldeed:tabularwith same mean and usage.
-
-
Forms usage
All
Forms.Checkerkeys must be passed with aform_prefix. All fields are considered unless aform: falseis specified.See
pwix:formsdocumentation for the list of available keys.
Methods
-
Field.Def.set( attribs<Object> )Merge a new definition part with the existing one.
-
Field.Def.toForm()Returns a columns specification suitable to Forms setup.
A field which have a
form = falsekey/value pair is ignored when building the fields definition.All
Field.Defdefinitions are considered when building the forms definition, unless:-
no
nameis set -
or a
form = falsekey/value pair is specified.
If none of the two above conditions are met, then the method returns at least an empty object.
-
-
Field.Def.toHelp()Extract and returns the help data, which may be an empty object.
All
Field.Defdefinitions are considered when building the help data, unless:-
no
nameis set -
or a
help: falsekey/value pair is specified.
If none of the two above conditions are met, then the method returns at least an empty object.
-
-
Field.Def.toTabular()Returns a column definition suitable to Datatable initialization.
A field which have a
tabular = falsekey/value pair is ignored when building the tabular definition. -
Field.Def.toSchema()Returns an object with fields definitions suitable to instanciate a SimpleSchema.
All
Field.Defdefinitions are considered when building a schema, unless:-
no
nameis set -
or a
schema = falsekey/value pair is specified.
-
Field.Set
An ordered collection of Field.Def objects.
It should be instanciated by the caller with a list or an array of fields definitions as plain javascript objects.
1 app.fieldsSet = new Field.Set( 2 { 3 name: '_id', 4 type: String, 5 // not considered in the tabular displays 6 tabular: false 7 }, 8 { 9 name: 'emails', 10 type: Array, 11 optional: true, 12 // not visible in the tabular displays 13 dt_visible: false 14 }, 15 { 16 name: 'emails.$', 17 type: Object, 18 optional: true, 19 // not considered in the tabular displays 20 // really useless as emails is subscribed to anyway 21 tabular: false 22 }, 23 { 24 name: 'emails.$.address', 25 type: String, 26 regEx: SimpleSchema.RegEx.Email, 27 dt_data: false, 28 // the title of the header 29 dt_title: pwixI18n.label( I18N, 'list.email_address_th' ), 30 dt_template: Meteor.isClient && Template.email_address, 31 // check function 32 form_check: AccountsManager.check?.emailAddress, 33 // if the email address is optional ? 34 form_checkType: 'optional' 35 }, 36 { 37 name: 'emails.$.verified', 38 type: Boolean, 39 dt_data: false, 40 dt_title: pwixI18n.label( I18N, 'list.email_verified_th' ), 41 dt_template: Meteor.isClient && Template.email_verified, 42 form_check: AccountsManager.check?.emailVerified 43 }, 44 { 45 dt_template: Meteor.isClient && Template.email_more 46 }, 47 { 48 name: 'username', 49 type: String, 50 optional: true, 51 dt_title: pwixI18n.label( I18N, 'list.username_th' ), 52 form_check: AccountsManager.check?.username 53 }, 54 { 55 name: 'profile', 56 type: Object, 57 optional: true, 58 blackbox: true, 59 tabular: false 60 }, 61 Notes.field({ 62 name: 'userNotes', 63 dt_title: pwixI18n.label( I18N, 'list.user_notes_th' ), 64 //dt_template: Meteor.isClient && Notes.template 65 }) 66 );
Both all fields of a Mongo document, all columns of a tabular display based on this collection, and all fields managed in an editing panel must be defined here. Hence the different definitions.
Methods
-
Field.Set.byName( name )Returns the named
Field.Defobject, or null.Because the
namekey is optional when defining a field, then not all field's are retrievable by this method. -
Field.Set.byPrefix( prefix )Returns the list of
Field.Defobjects which contains at least one key which starts by the given prefix, or an empty array.prefixdefaults to abn empty string, which means all fields. -
Field.Set.extend( <Array|Object> )Extends the
Field.Setset with the provided fields definitions, as an object, or an array of objects, where each object has following keys:-
before: the optional column reference name to be inserted before, must be already defined.If a column name is not specified, then the provided fields definitions are appended to the existing ones.
-
fields: an array of field definitions.
-
-
Field.Set.indexByName( name )Returns the index of the named column, or -1.
Because the
namekey is optional when defining a field, then not all field's are retrievable by this method. -
Field.Set.names()Returns the array of defined
name's. -
Field.Set.toForm()Returns an ordered list of columns definitions suitable to Forms setup.
-
Field.Set.toHelp()Extract and returns a keyed object with the help data.
-
Field.Set.toTabular()Returns an ordered list of columns definitions suitable to Datatable initialization.
-
Field.Set.toSchema()Returns a field definition suitable to instanciate a SimpleSchema.
Configuration
The package's behavior can be configured through a call to the Field.configure() method, with just a single javascript object argument, which itself should only contains the options you want override.
Known configuration options are:
-
prefixesA list of prefixes used by the application.
The package needs to know them in order to filter them before sending to schema or tabular or...
Default to an empty array.
-
verbosityDefine the expected verbosity level.
The accepted value can be any or-ed combination of following:
-
Field.C.Verbose.NONEDo not display any trace log to the console
-
Field.C.Verbose.CONFIGURETrace
Field.configure()calls and their result
-
Please note that Field.configure() method should be called in the same terms both in client and server sides.
Remind too that Meteor packages are instanciated at application level. They are so only configurable once, or, in other words, only one instance has to be or can be configured. Addtionnal calls to Field.configure() will just override the previous one. You have been warned: only the application should configure a package.
Field.configure() is a reactive data source.
NPM peer dependencies
Starting with v 0.3.0, and in accordance with advices from the Meteor Guide, we no more hardcode NPM dependencies in the Npm.depends clause of the package.js.
Instead we check npm versions of installed packages at runtime, on server startup, in development environment.
Dependencies as of v 1.5.0:
1 'lodash': '^4.17.0'
Each of these dependencies should be installed at application level:
meteor npm install <package> --save
Translations
None at the moment.
Cookies and comparable technologies
None at the moment.
Issues & help
In case of support or error, please report your issue request to our Issues tracker.
P. Wieser
- Last updated on 2024, Nov. 19th