Resource class for managing API resources, the HTTP client for making requests, and the urls object for accessing backend URLs.
Resource System
The Resource system provides a structured way to interact with REST APIs usingModel and Collection classes for data management.
Resource Class
TheResource class represents a single API endpoint and provides caching, fetching, and model instantiation.
Source: packages/kolibri/apiResource.js:578
Constructor Options
- name (required): The Django REST API resource name
- namespace: The Django app namespace (default:
'core') - idKey: The primary key field name (default:
'id')
Model Class
Represents a single resource instance with fetching, saving, and deleting capabilities. Source:packages/kolibri/apiResource.js:12
Model Properties
- id: The model’s primary key
- data: A clean copy of model attributes (read-only)
- attributes: Raw model attributes
- synced: Whether the model is synced with the server
- new: Whether the model exists on the server
Model Methods
fetch(force = false) Fetches model data from the server.- force: If
true, fetch even if already synced - Returns: Promise resolving to model data
- attrs: Object of attributes to save
- exists: Override the new/existing detection
- Returns: Promise resolving to updated model data
- Returns: Promise resolving to the model ID
Collection Class
Represents a collection of models from a list endpoint. Source:packages/kolibri/apiResource.js:262
Collection Properties
- models: Array of Model instances
- data: Array of model data (or object with
resultsif paginated) - metadata: Additional response metadata (e.g., pagination info)
- synced: Whether collection is synced with server
Collection Methods
fetch(force = false) Fetches collection data from the server.Resource Methods
Convenience Methods
fetchModel() Fetch a single model by ID.Simple CRUD Methods
For simple use cases without caching:Custom Endpoints
For custom viewset actions: List endpoints (no ID required):HTTP Client
Theclient module provides an HTTP client based on Axios.
Source: packages/kolibri/client.js:70
Client Options
- url: Request URL (required)
- method: HTTP method (
'get','post','patch','delete', etc.) - data: Request body data
- params: URL query parameters
- headers: Custom headers
- multipart: If
true, send as multipart/form-data
Built-in Features
- Automatic disconnection detection
- Login timeout detection
- CSRF token handling
- Request cancellation when offline
URLs Object
Theurls object provides access to Django backend URLs.
Source: packages/kolibri/urls.js
URL Naming Convention
URLs follow the pattern:kolibri:{namespace}:{model}_{action}
- namespace: Django app namespace (e.g.,
core,content) - model: Model name (lowercase)
- action:
list,detail, or custom action name
kolibri:core:session_listkolibri:core:session_detailkolibri:content:contentnode_list
Real-World Examples
Defining a Resource
Fetching and Displaying Data
Creating and Saving
Using Cached Models
Working with Custom Endpoints
Best Practices
- Always use Resource classes for API calls - never use raw
fetchor direct axios - Define resources once in dedicated
apiResources/files - Leverage caching with
getModel()andgetCollection()for frequently accessed data - Use convenience methods (
fetchModel,saveModel) for simple operations - Handle errors appropriately - promises will reject on HTTP errors
- Use getParams for filtering collections consistently
- Check model.synced before assuming data freshness