Search code examples
javascriptbackbone.jspugbackbone-forms

Backbone form - custom template for a field - not submitting the value


SO i have a checkbox in my template which is (jade)

input(type='checkbox' name='flag' class='pure-checkbox onoffswitch-checkbox' id='myonoffswitch')

and i use this custom template form my flag field

  schema:
    flag:
      template: require ('templates/onoff-switch.hbs')
    threshold:
      type: 'Text'
      title: 'Threshold($)'
      editorClass: 'pure-input-1-3'

but when i submit my form the flag value is not set, its empty. I hope i have to bind my value to the checkbox value but i am sure how.

i tried

    input(type='checkbox' name='flag' class='pure-checkbox onoffswitch-checkbox' id='myonoffswitch' checked="{{#if flag}}checked{{/if}}")

Can someone help?


Solution

  • Create a custom form editor called 'switch' extending Backbone.Form.editors.Checkbox and use your own template in it. Something like

    Backbone.Form.editors.Switch = Backbone.Form.editors.Checkbox.extend
      tagName: 'div'
      className: 'onoffswitch'
      template: require('templates/onoff-switch.hbs')

    Refer Backbone Forms https://github.com/powmedia/backbone-forms for more information on Custom editor and how to extend Checkbox.