apostrophe-schemas
Advanced tools
Comparing version
61
index.js
@@ -127,2 +127,5 @@ /* jshint node:true */ | ||
}); | ||
_.each(schema, function(field) { | ||
delete field.group; | ||
}); | ||
@@ -224,2 +227,31 @@ // Check for groups and fields with the same name, which is | ||
// Return a new schema containing only the fields named in the | ||
// `fields` array, while maintaining existing group relationships. | ||
// Any empty groups are dropped. Do NOT include group names | ||
// in `fields`. | ||
self.subset = function(schema, fields) { | ||
var schemaSubset = _.filter(schema, function(field) { | ||
return _.contains(fields, field.name) || (field.type === 'group'); | ||
}); | ||
// Drop empty tabs | ||
var fieldsByGroup = _.groupBy(schemaSubset, 'group'); | ||
schemaSubset = _.filter(schemaSubset, function(field) { | ||
return (field.type !== 'group') || (_.has(fieldsByGroup, field.name)); | ||
}); | ||
// Drop references to empty tabs | ||
_.each(schemaSubset, function(field) { | ||
if (field.group && (!_.find(schemaSubset, function(group) { | ||
return ((group.type === 'group') && (group.name === field.group)); | ||
}))) { | ||
delete field.group; | ||
} | ||
}); | ||
return schemaSubset; | ||
}; | ||
// Return a new object with all default settings defined in the schema | ||
@@ -236,2 +268,24 @@ self.newInstance = function(schema) { | ||
self.subsetInstance = function(schema, instance) { | ||
var subset = {}; | ||
_.each(schema, function(field) { | ||
if (field.type === 'group') { | ||
return; | ||
} | ||
if (!self.copiers[field]) { | ||
// These rules suffice for our standard fields | ||
subset[field.name] = instance[field.name]; | ||
if (field.idField) { | ||
subset[field.idField] = instance[field.idField]; | ||
} | ||
if (field.idsField) { | ||
subset[field.idsField] = instance[field.idsField]; | ||
} | ||
} else { | ||
self.copiers[field](name, instance, subset, field); | ||
} | ||
}); | ||
return subset; | ||
}; | ||
// Determine whether an object is empty according to the schema. | ||
@@ -309,3 +363,3 @@ // Note this is not the same thing as matching the defaults. A | ||
if (typeof(data[name]) !== 'array') { | ||
if (!Array.isArray(data[name])) { | ||
object[name] = []; | ||
@@ -535,3 +589,3 @@ return setImmediate(callback); | ||
self.converters.form.checkboxes = function(req, data, name, object, field, callback) { | ||
if (typeof(data[name]) != 'array') { | ||
if (!Array.isArray(data[name])) { | ||
object[name] = []; | ||
@@ -843,2 +897,3 @@ return setImmediate(callback); | ||
self.empties[type.name] = type.empty; | ||
self.copiers[type.name] = self.copier; | ||
}; | ||
@@ -858,2 +913,4 @@ | ||
self.copiers = {}; | ||
if (callback) { | ||
@@ -860,0 +917,0 @@ return callback(null); |
{ | ||
"version": "0.5.55", | ||
"version": "0.5.56", | ||
"name": "apostrophe-schemas", | ||
@@ -4,0 +4,0 @@ "description": "Schemas for easy editing of properties in Apostrophe objects", |
@@ -31,3 +31,4 @@ # apostrophe-schemas | ||
* [Creating Schemas With Compose](#creating-schemas-with-compose) | ||
* [Refining Existing Schemas With Refine](#refining-existing-schemas-with-refine) | ||
* [Refining existing schemas with refine](#refining-existing-schemas-with-refine) | ||
* [Filtering existing schemas with subset](#filtering-existing-schemas-with-subset) | ||
@@ -796,2 +797,12 @@ `apostrophe-schemas` adds support for simple schemas of editable properties to any object. Schema types include text, select, apostrophe areas and singletons, joins (relationships to other objects), and more. This module is used by the `apostrophe-snippets` module to implement its edit views and can also be used elsewhere. | ||
And, if our field modifies properties other than the one matching its `name`, we must supply a `copier` function so that the `subsetInstance` method can be used to edit personal profiles and the like: | ||
```javascript | ||
copier: function(name, from, to, field) { | ||
// Note: if this is really all you need, you can skip | ||
// writing a copier | ||
to[name] = from[name]; | ||
} | ||
``` | ||
The `views/schemaList.html` template should look like this. Note that the "name" and "label" options are passed to the template. In fact, all properties of the field that are part of the schema are available to the template. Setting `data-name` correctly is crucial. Adding a CSS class based on the field name is a nice touch but not required. | ||
@@ -996,1 +1007,29 @@ | ||
The options are exactly the same as the options to `compose`. The returned array is a copy. No modifications are made to the original schema array. | ||
#### Filtering existing schemas with `subset` | ||
If you just want to keep certain fields in your schema, while maintaining the same tab groups, use the `subset` method. This method will discard any unwanted fields, as well as any groups that are empty in the new subset of the schema: | ||
```javascript | ||
// A subset suitable for people editing their own profiles | ||
var profileSchema = schemas.subset(schema, [ 'title', 'body', 'thumbnail' ]); | ||
``` | ||
If you wish to apply new groups to the subset, use `refine` and `groupFields`. | ||
#### Creating new objects with `newInstance` | ||
The `newInstance` method can be used to create an object which has the appropriate default value for every schema field: | ||
```javascript | ||
var snowman = schemas.newInstance(snowmanSchema); | ||
``` | ||
#### Filtering object properties with `subsetInstance` | ||
The `subsetInstance` method accepts a schema and an existing instance object and returns a new object with only the properties found in the given schema. This includes not just the obvious properties matching the `name` of each field, but also any `idField` or `idsField` properties specified by joins. | ||
```javascript | ||
var profileSchema = schemas.subset(people.schema, [ 'title', 'body', 'thumbnail' ]); | ||
var profile = schemas.subsetInstance(person, profileSchema); | ||
``` |
110657
3.21%1504
3.37%1033
3.92%