We are building a simple status board like Facebook. Made a custom object called Status__c. It has a custom field called StatusText__c (Text Area).
Step 1: Write an Apex Controller to Fetch Data from DB
We are going to call this one “StatusService” because it provides useful static methods for working with the Status__c object. Remember it’s an apex class.
Step 2: Initialize a Lightning Component
Then we are going to go straight to our controller. Think of a controller like an Xbox Controller – We want something to run when we press the A button, we want something to run when it’s plugged in (initialized), etc. When you look at your controller, you should see all the available buttons you can press / actions you can take + initialization (doInit).
Step 3: Make a JS Controller for your Component
Define all the functions you will need to call in here.
This block of JS calls the getStatuses function that we defined in our Apex Controller.
It then sets our JS variables based on what the server returned.
And that is just one function! Separate multiple functions by a comma.
Step 4: Build out your lightning component
- You just defined the functions you need, now you can call those functions from your page!
- Set the controller to the Apex Controller that you made. We called ours StatusService.
- Define any attributes (variables) that you are going to use in your page with a name and a type.
- Output any variables that you defined earlier
Step 5: Slap Your Component into a Lightning App
Alright. Now you can pull and display info from the DB within the lightning components framework. In part 2, We are going to get some user input, and write it to the DB.