Kiss application manager
This module allows to:
- define and access models
- define and access collections
- define views
- define controllers
- init the application using kiss.app.init()
Once the models and collections are defined, they are stored in the app object.
// Getting models and collections
const appModels = kiss.app.models
const appCollections = kiss.app.collections
// Getting a model definition or a collection
const userModel = appModels["user"]
const userCollection = appCollections["user"]
// Using the model
const Bob = userModel.create({firstName: "Bob", lastName: "Wilson"})
await Bob.save()
// Using the collection if you're not sure it's loaded in memory (async method)
const John = await userCollection.findOne("123")
// Using the collection if it's already loaded in memory (sync method)
const Will = userCollection.getRecord("456")
Members
# static collections
Store the application collections. More about collections here.
Example
const userCollection = kiss.app.collections["user"]
// Or...
const userCollection = kiss.app.collections.user
# static models
Store the application models. More about models here.
Example
const userModel = kiss.app.models["user"]
// Or...
const userModel = kiss.app.models.user
Methods
# static defineModel(model) → {Model}
Define a new model in the application
This automatically:
- references the model in kiss.app.models
- references a new collection for the model in kiss.app.collections
- references a new record class to instanciate the model
- generates the basic api for the record class (create, save, update, delete)
- generates getters/setters for record's computed fields (= virtual fields)
Check the class Model documentation for more informations about models.
Parameters:
Name | Type | Description |
---|---|---|
model |
object
|
The model configuration object |
The newly defined model
Model
# static defineModelRelationships()
Define the model relationships
This methods explores all the application models and finds automatically the relationships between the models. When exploring the models, specific field types are generating the relationships:
- link: field used to link one or many foreign records
- lookup: field that lookups a field in a foreign record
- summary: field that lookups and summarize data from multiple foreign records
Example
kiss.app.defineModelRelationships()
# static definePlugin(plugin)
Add a plugin definition to the application
Parameters:
Name | Type | Description |
---|---|---|
plugin |
object
|
# static defineTexts(texts)
Define all the application texts that can be translated. Each text will be used as an English text if it doesn't have any english translation.
Parameters:
Name | Type | Description |
---|---|---|
texts |
object
|
Example
kiss.app.defineTexts({
"hello": {
fr: "bonjour"
},
"#thank": {
en: "thank you",
fr: "merci"
}
})
# static defineView(config)
Define a view by storing its renderer function into the list of view renderers. It does NOT store a view, but instead stores a view 'renderer' function that will generate the view later, when needed.
Parameters:
Name | Type | Description |
---|---|---|
config |
object
|
|
id |
string
|
The id of the view to add |
renderer |
function
|
The function that will render the view when needed |
meta |
object
|
Meta informations injected in the HTML header. Can be localized or not. See examples in kiss.views module. |
Example
kiss.app.defineView({
id: "home",
renderer: function (id, target) {
// ... build your view here
return createPanel({
id, // Very important. Can't work without it.
target, // Optional insertion point in the DOM. You can omit if you don't need it.
title: "My first panel",
// A few panel properties
draggable: true,
closable: true,
width: 300,
height: 200,
boxShadow: "5px 5px 10px #cccccc",
// Panel content
items: [
{
type: "html",
html: "<center>Hello world?</center>",
// W3C events attached to the html element
// It works only if you've attached the "hello" viewController (see below)
events: {
onclick: function() {
$(id).hello()
}
}
}
]
})
}
})
# static defineViewController(id, viewController)
Define a controller for a specific view
The view controller must have the same name as the controlled view. They will be paired automatically.
Parameters:
Name | Type | Description |
---|---|---|
id |
string
|
|
viewController |
object
|
Object containing all the controller methods |
Example
// This controller has 4 methods, hello(), world(), foo() and bar()
kiss.app.defineViewController("home", {
hello: function() {
createNotification({
message: "Hello!",
duration: 2000
})
},
// ... or using an arrow function:
world: () => console.log("World!"),
// ... or class member notation:
foo() {
console.log("Foo!")
},
// Methods can be async, too:
async bar() {
return await 42
}
})
# static getCollectionByModelName(modelName) → {object}
Get a Collection by its model's name
Collections are normally retrieved by id like this:
const projectCollection = kiss.app.collections[collectionId]
const projects = await projectCollection.find()
In some situation, we just know the name of the collection's model, so we have to use this method:
const projectCollection = kiss.app.getCollectionByModelName("Project")
const projects = await projectCollection.find()
Parameters:
Name | Type | Description |
---|---|---|
modelName |
string
|
The name of the collection's model (case insensitive) |
Collection
object
# static getModel(modelId) → {object}
Get a Model by id or by name
Note: if the model is not found, the methods tries to find the model by its name
Parameters:
Name | Type | Description |
---|---|---|
modelId |
string
|
id or name (case insensitive) of the model |
Model
object
# static getModelByName(modelName) → {object}
Get a Model by name
Models are normally retrieved by id like this:
const projectModel = kiss.app.models[modelId]
const projectRecord = projectModel.create({projectName: "foo"})
In some situation, we just know the model name and have to use this method:
const projectModel = kiss.app.getModelByName("Project")
const projectRecord = projectModel.create({projectName: "foo"})
Parameters:
Name | Type | Description |
---|---|---|
modelName |
string
|
The name of the model (case insensitive) |
Model
object
# static init()
Init every kiss modules at startup:
- init the theme (color/geometry)
- init the language
- init the screen size observer
- restore the session (if any)
- init the client router
- navigate to the first application view (given by the hash parameter #ui=APP_FIRST_ROUTE)
# static listCollections()
List all the application collections. Give their name and number of records.