Previously, I showed my top seven snippets. Let us use some of the snippets together to make a new component.
To follow along, you should install the snippets.
Next, we will consider our output. We will design a form bean component for a user management form consisting of the following values:
- FullName
- Status
- Username
Create a new component called snippet.cfc. Paste the four attributes listed above three times in snippet.cfc . Your file should look like the one below:
Email
Fullname
Status
Username
Email
Fullname
Status
Username
Email
Fullname
Status
Username
Begin at the last block. This will be as block of getters and setters. To be as efficient as possible, put your cursor at the beginning of Email, select the word, cut it, type getset and your snippet key combo. (Either CTRL+SHIFT+. or CTRL+J). When the prompt appears, paste the text Email, then type an s character in each box to select ‘string’. Repeat this for each of the four attributes.
When complete, and with some judicious spacing, you end up with this:
Email
Fullname
Status
Username
Email
Fullname
Status
Username
Not too shabby eh? Now we need an init function. Cut both sets of remaining attributes into the clipboard. Type fun and your snippet trigger text. When the dialogue box appears, fill in the following information:
Hint: I perform initialization for this component
Name: init
Access: public
Return Type: snippet
Then paste the attributes. Now for the first four attributes we will work similar to our getset block by cutting the attribute, then fill out the information as so:
Type: String
Required: False
After you close the dialogue box, the cursor should be inside the name attribute. Paste the attribute then wash, rinse and repeat. After you finish each of the four, type default=”” and copy paste it into the argument tags. Your file should look like such:
Email
Fullname
Status
Username
We will finish off the component by adding the cfset tags that execute the setters. Paste this block 4 times:
Then put the attribute in between set and () as well as after the arguments. Finally add the return statement. The specific block should look like this:
Congratulations! Wasn’t that fun?
When complete, you should have a nice Form Bean ready to use. For posterity, the complete code looks like this: