Overview
TheSingleController extends BaseController and is designed for managing single-instance documents - documents that can only have one instance in the system. This controller automatically redirects list actions to update actions and provides specialized rendering for single documents.
Class Definition
packages/loopar/core/controller/single-controller.js:6
When to Use
- For documents that should only have one instance system-wide (e.g., Settings, Configuration)
- When you need to prevent creation of multiple records
- For system-level settings or global configurations
- When working with singleton patterns in your application
Key Features
- Automatic redirection from list to update view
- Specialized document rendering with parent menu tracking
- Integration with web app menu structure
- Single-instance enforcement at controller level
Constructor
Configuration object containing controller initialization parameters including:
action: The current action being performeddocument: The document type being controlleddata: Request data- Other inherited controller properties
Methods
getParent()
Retrieves the parent menu item for the current document.Promise<string> - The parent page identifier from the Menu Item
Implementation:
packages/loopar/core/controller/single-controller.js:13
sendAction(action)
Handles action dispatching for the controller, checking for method existence before execution.The action name to execute (e.g., ‘view’, ‘update’, ‘search’)
Promise<object> - The result of the action execution
Behavior:
- Executes
beforeAction()for authentication/authorization - Capitalizes action name and prepends ‘action’ (e.g., ‘view’ becomes ‘actionView’)
- If method exists, executes it
- Otherwise, delegates to
sendDocument(action)
packages/loopar/core/controller/single-controller.js:17
actionView()
Handles the view action by rendering the single document.Promise<object> - Rendered document response
Source: packages/loopar/core/controller/single-controller.js:27
sendDocument(action)
Retrieves and renders a single document with web app integration.The document or action identifier to render
Promise<object> - Rendered document with metadata including:
Entity: Document entity information with name, background image, and structureactiveParentMenu: Parent menu item reference__DOCUMENT_TITLE__: Display title from menu or document name
- Retrieves web app menu items
- Matches action against menu pages or links
- Loads the corresponding document
- Enriches document with navigation context
- Renders with specialized metadata
packages/loopar/core/controller/single-controller.js:31
Usage Examples
Creating a Single Document Controller
Accessing a Single Document
Custom Action in Single Controller
Inherited Methods
SingleController inherits all methods from:- BaseController - CRUD operations, list management
- CoreController - Rendering, error handling, authentication
- AuthController - User authentication and authorization
Key Inherited Methods
actionUpdate()- Update the single documentrender(meta)- Render the document viewsuccess(message, options)- Return success responseerror(message, options)- Return error responseredirect(url)- Redirect to another URLbeforeAction()- Authentication and authorization checks
Properties
Current action being executed (inherited from BaseController)
Document type name being controlled
Request data object
Default action (inherited from BaseController, typically redirected to ‘update’)
Whether to display sidebar navigation (inherited from BaseController)
Best Practices
Single Instance Pattern: Always ensure your document entity is marked as
is_single: true to prevent creation of multiple instances.Do’s
- Use SingleController for system-wide settings and configurations
- Override
sendDocument()if you need custom rendering logic - Implement custom actions for specific single-document operations
- Leverage parent menu integration for navigation context
Don’ts
- Don’t try to create new instances with
actionCreate()- it will fail - Don’t expect list views to render normally
- Don’t use for documents that need multiple instances
- Avoid bypassing the redirect logic in constructor
Related Controllers
- BaseController - Parent class with CRUD operations
- PageController - Extends SingleController for page views
- FormController - For form-specific document handling