Bootcamp

From idea to product, one lesson at a time. To submit your story: https://tinyurl.com/bootspub1

Follow publication

Using Knack to Generate QR Codes with JavaScript

If you’re a developer, you know that one of the most important parts of your job is to ensure your applications are well-designed. After all, a poorly designed app can be confusing for users and lead to a lot of headaches for you down the line. That’s why it’s vital to use a platform like Knack that makes designing beautiful, user-friendly apps easy.

One neat feature of Knack is that it’s actually quite easy to create a QR code generator that you can then use to transfer data from custom fields. This requires JavaScript, and it’s simple to set up following this guide.

Why Should You Use JavaScript for Your App?

JavaScript automation opens up a whole world of possibilities for customizing your Knack apps. Here are just a few examples:

  • Add custom validation rules to form fields
  • Send SMS notifications when certain events occur in your app
  • Process payments using Stripe or another payment processor
  • Generate PDFs from data in your app
  • Import data from CSV files or other data sources

These are just a few examples — the possibilities are really only limited by your imagination. From generating QR codes for customer loyalty programs to sending push notifications when a new record is added to your app, JavaScript automation can help you take your app to the next level. So if you can dream it up, chances are you can build it using JavaScript automation.

How to Generate QR Codes with JavaScript

Now that we’ve covered some of the many advantages of using JavaScript with Knack, here’s an example of how to generate QR codes. Businesses can find this useful for many purposes. QR codes hold more than just URLs. They can also contain:

  • Contact information
  • Calendar data
  • Location data
  • Application commands
  • Plain text

When used correctly, QR codes can automate repetitive tasks. For example, instead of manually entering data at an event ticket gate or a customer checkout counter, employees can simply scan a customer’s QR code to auto-populate the necessary fields. If you put this functionality into your app, the possibilities are endless.

Source: https://www.pexels.com/photo/close-up-photo-of-programming-of-codes-546819/

Step 1: Create an object in the Knack database

Include any relevant fields. This Knack object will store all of the information read in the QR code.

Step 2: Create a form to contain information from the QR codes

This is where information will populate after your app reads a QR code.

Step 3: Insert the following JavaScript code

You can access the code here or copy it below. Of course, make sure you are in the JavaScript tab before you paste the code.

const openQRCamera = function (node, callback) {     Knack.showSpinner();     var reader = new FileReader();     reader.onload = function() {        node.value = "";        qrcode.callback = function(res) {           if(res instanceof Error) {              alert("No QR code found. Please make sure the QR code is within the camera's frame and try again.");              callback(null);           } else {              callback(res)           }        };        qrcode.decode(reader.result);     };     reader.readAsDataURL(node.files[0]);   };   const insertQRScannerButton = function (elAfter, qrDataCallback){     $('<label>')     .text('Scan QR Code')     .addClass('qrcode-text-btn')     .append(        $('<input>')        .attr({           'type': 'file',           'accept': 'image/*',           'capture':'environment',           'tabindex': '-1'        })        .change(function () {           openQRCamera(this, function (flightData) {              qrDataCallback(flightData);           });        })     )     .insertAfter(elAfter);   };   $(document).on('knack-view-render.view_373', function (event, view, data) {     LazyLoad.js(['https://s3.amazonaws.com/soluntech/gat/qr-scanner.js 5'], function () {        var form = $('form');        var setData = function (flightData) {           if (!flightData) {              return Knack.hideSpinner();           }           form.find('#field_408').val(flightData);           Knack.hideSpinner();        };        insertQRScannerButton(form.find('#kn-input-field_408'), setData);     });   });

Link to the library: https://s3.amazonaws.com/soluntech/gat/qr-scanner.js 5 (file cloned from https://github.com/LazarSoft/jsqrcode and uploaded to soluntech s3).

In the render-view event, load the library using Knack’s LazyLoad.js function.

The insertQRScannerButton function inserts a new input type file component which will execute the openQRCamera function that will allow you to open the cell phone camera to take the QR Code photo.

Step 4: Read the image

You’re nearly done! This code will generate an image, but now you need your app to decode it. Use the native JavaScript FileReader with the decode function (qrcode.decode) of the library. This will interpret your photo by finding the QR code before sending the result to the callback function(qrcode.callback).

Step 5: Fill Out the Form

Your information should now go to the form you created. If nothing populates, it’s because the QR code was invalid. Alternatively, you can edit the qrcode.callback code so that the information collected goes directly into the database using the API without needing a form.

Some Best Practices to Keep in Mind When Designing Your App

Source: https://www.pexels.com/photo/space-gray-iphone-6-54284/

This is one low-cost way to create an app with JavaScript. No matter what you decide to do, here are some best practices for the most successful apps.

  • Ensure the user interface (UI) is simple and easy to use. This is especially important if the application will be used by a wide range of people, as a complex UI can be confusing and off-putting.
  • Clearly define the purpose of the application before starting to design it. This will ensure that the application is built with the right functionality in mind and meets the users’ needs.
  • Finally, when the application is complete, it is important to test it thoroughly. This will ensure that the application works as intended and that there are no errors.

By following these best practices, you can be sure your application will succeed when using Knack with JavaScript automation. Ultimately, this will lead to a better user experience and a more streamlined way to work with your data up close.

Conclusion

Designing applications in Knack with JavaScript automation can save you a lot of time and effort. It can also help you create more sophisticated applications that are better suited to your specific needs. If you are unfamiliar with JavaScript, many resources are available online to help you learn the basics.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bootcamp
Bootcamp

Published in Bootcamp

From idea to product, one lesson at a time. To submit your story: https://tinyurl.com/bootspub1

Aaron Smith
Aaron Smith

Written by Aaron Smith

Aaron Smith is a content strategist. He covers tech, health and wellness. He has a particular interest in alternative family planning.

No responses yet

Write a response