• Home
  • Why Erstwhile?
  • Getting Started
  • Learn Erstwhile
  • API Docs
  • Home
  • Why Erstwhile?
  • Getting Started
  • Learn Erstwhile
  • API Docs
  • Learning Erstwhile
  • File Structure
  • Compilation Process
  • Routing
  • Request Flow
  • Components and ErML
    • How Components are Compiled
    • Themes
    • Layouts
  • Scoped Variables
  • Modals
  • Models
    • Using an Erstwhile-aware Backend
    • Creating Your Own Description.json
    • Using Models from the Controller

Using Models from the Controller

13 views 0

Written by mike
August 9, 2023

Using Erstwhile models within the controller is pretty straightforward:

// in this case, gather the form data from the form
let formValues = $App.getComponent("create-todo").getValues();

// get a reference to the model
let model = $App.getModel('Todo');

/*
 * Call the create method on the model. The first argument is the data.
 * The return value is an axios promise, so you can handle that however
 * you would like.
 */
model.create(formValues).then(function(response) {
  if(response.data.success === false) {
    $App.scopes.modal.message = "There were errors in your form";
    $App.getComponent('create-todo').showErrors(response.data);
    $App.getComponent("create-todo-alert").show();
  } else {
    $App.getComponent("create-todo-alert").hide();
    $App.getComponent('create-todo').clearErrors();
    $App.closeModal();
    $App.getComponent('todo-list-table').redraw();
  }
}).catch(function(e) {
  console.log("error", e)
});

The developer doesn’t have to worry if the particular endpoint is GET, POST or whatever because the framework takes care of it. The first argument will automatically be the response body or the query string, depending on the need.

Sometimes an endpoint will also require adding parameters to the path. Erstwhile handles this too.

// get a reference to the model
let model = $App.getModel('Todo');

// pass in an empty first argument, and a map with the necessary data in the second
model.get({}, {todoID: args.id}).then(function(todoResponse) {
  $App.scopes.page.todo = todoResponse.data;
  $App.scopes.page.title = `Edit ${todoResponse.data.title}`;
}).catch(function(e) {
  console.log("error", e)
});

 

Was this helpful?

Yes  No
Related Articles
  • Creating Your Own Description.json
  • Using an Erstwhile-aware Backend
  • Models
  • Modals
  • Scoped Variables
  • Layouts

Didn't find your answer? Contact Us

Previously
Creating Your Own Description.json

loader

Join our Mailing List     

  • Privacy Policy
  • Terms of Use
  • © 2025 RestlessDev. Made in New Orleans.

Welcome to Erstwhile

Thank you for checking out Erstwhile. We’re just getting started, so if you’d like to keep up with our progress please register now and we’ll let you know when new releases come out.

loader

Email Address*

Name