Skip to content
This repository has been archived by the owner on Sep 5, 2018. It is now read-only.

Enable a name remapping feature, remap plural models to singular #75

Merged
merged 2 commits into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build/js/wp-api.min.js.map

This file was deleted.

41 changes: 40 additions & 1 deletion js/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,42 @@
},

constructFromSchema: function() {
var routeModel = this, modelRoutes, collectionRoutes, schemaRoot, loadingObjects;
var routeModel = this, modelRoutes, collectionRoutes, schemaRoot, loadingObjects,

/**
* Set up the model and collection name mapping options. As the schema is built, the
* model and collection names will be adjusted if they are found in the mapping object.
*
* Localizing a variable wpApiSettings.mapping will over-ride the default mapping options.
*
*/
mapping = wpApiSettings.mapping || {
models: {
'Categories': 'Category',
'Comments': 'Comment',
'Pages': 'Page',
'PagesMeta': 'PageMeta',
'PagesRevisions': 'PageRevision',
'Posts': 'Post',
'PostsCategories': 'PostCategory',
'PostsRevisions': 'PostRevision',
'PostsTags': 'PostTag',
'Schema': 'Schema',
'Statuses': 'Status',
'Tags': 'Tag',
'Taxonomies': 'Taxonomy',
'Types': 'Type',
'Users': 'User'
},
collections: {
'PagesMeta': 'PageMeta',
'PagesRevisions': 'PageRevisions',
'PostsCategories': 'PostCategories',
'PostsMeta': 'PostMeta',
'PostsRevisions': 'PostRevisions',
'PostsTags': 'PostTags'
}
};

/**
* Iterate thru the routes, picking up models and collections to build. Builds two arrays,
Expand Down Expand Up @@ -122,6 +157,7 @@
// If the model has a parent in its route, add that to its class name.
if ( '' !== parentName && parentName !== routeName ) {
modelClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
modelClassName = mapping.models[ modelClassName ] || modelClassName;
loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {

// Function that returns a constructed url based on the parent and id.
Expand Down Expand Up @@ -167,6 +203,7 @@

// This is a model without a parent in its route
modelClassName = wp.api.utils.capitalize( routeName );
modelClassName = mapping.models[ modelClassName ] || modelClassName;
loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {

// Function that returns a constructed url based on the id.
Expand Down Expand Up @@ -210,6 +247,7 @@
if ( '' !== parentName && parentName !== routeName ) {

collectionClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {

// Function that returns a constructed url passed on the parent.
Expand All @@ -235,6 +273,7 @@

// This is a collection without a parent in its route.
collectionClassName = wp.api.utils.capitalize( routeName );
collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {

// For the url of a root level collection, use a string.
Expand Down