marcoschwartz / aREST_UI

Embedded UI for the aREST framework
72 stars 34 forks source link

Reduce button click events #24

Open lyweilian opened 7 years ago

lyweilian commented 7 years ago

First of all great work with what you have done with aRestUI. It's awesome!

I have made some slight modifications to my local copy of your code that generates the button Javascript. Instead of having a pair of functions (on and off) per pin for buttons. You can simply have one button click event for all buttons. This can be achieved by changing the ID of the button to tell the function what pin and what state (low or high). This may help keep your code lean and keep the OUTPUT_BUFFER_SIZE size lower in aREST.h. You may want to also consider offloading the HTML generation to JQuery as well.

See the following:

// Buttons UI
    for (int i = 0; i < buttons_index; i++) {
      addToBuffer("<div class=\"row\">");
      addToBuffer("<div class=\"col-md-2\"><button class=\"btn btn-block btn-lg btn-primary\" id='btn_1_");
      addToBuffer(buttons[i]);
      addToBuffer("'>On</button></div>");
      addToBuffer("<div class=\"col-md-2\"><button class=\"btn btn-block btn-lg btn-danger\" id='btn_0_");
      addToBuffer(buttons[i]);
      addToBuffer("'>Off</button></div>");
      addToBuffer("</div>");
    }
    // Buttons JavaScript

    addToBuffer("$('.btn').click(function(){");
    addToBuffer("var id = $(this).attr('id').split('_');");
    addToBuffer("$.getq('queue','/digital/'+id[2]+'/'+id[1]); });");