Base.prototype.getModel >= 1.0.0
Purpose
Get a model instance and attach a conduit if possible
Syntax
Base#getModel
(
Stringmodel_name
Objectoptions
);
Parameters
model_name
- The name of the model to get
options
- Extra options
Return values
Model
Examples
Get a model from inside a controller action
Most Alchemy classes inherit from the Base class, so the getModel() method is almost always available. In the controller you'd do something like this:
var Blog = Function.inherits('Alchemy.Controller.App', function Blog(conduit) {
Blog.super.call(this, conduit);
});
Blog.setAction(async function index(conduit) {
var blog_model = this.getModel('Blog');
var posts = await blog_model.find('all');
this.set('posts', posts);
this.render('blog/index');
});
In this specific case you wouldn't even need to do this, as the Blog model will already be available in the Blog controller under the model property.
Comments