Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I looked into the <a href="http://view.jquerymobile.com/1.3.2/dist/demos/faq/how-can-i-stop-auto-enhancement-of-elements.html" rel="nofollow">jQuery Mobile Demos</a> again and found <code>data-role="none"</code>. It prevents JQM from auto-enhancing an element. Now I can add checkboxes and let the BindingHandler enhance them through <code>checkbox.checkboxradio()</code> afterwards.</p> <p>Now the checkbox looks like this: </p> <pre><code>&lt;button data-bind="click: add"&gt;Add checkbox&lt;/button&gt; &lt;!-- show checked boxes --&gt; Checked: &lt;div id="checkedBoxes" data-bind="foreach: boxes"&gt; &lt;span data-bind="text: name, if: checked"&gt;&lt;/span&gt; &lt;/div&gt; &lt;hr&gt; &lt;!-- checkboxes --&gt; &lt;div id="newBoxes" data-bind="foreach: boxes"&gt; &lt;label&gt; &lt;input type="checkbox" data-bind="checkbox: checked" data-role="none" /&gt; &lt;span data-bind="text: name"&gt;&lt;/span&gt; &lt;/label&gt; &lt;/div&gt; </code></pre> <p>And in the BindingHandlers <code>init</code> method, <code>checkbox.removeAttr('data-role')</code> is used to let JQM enhance the element via <code>checkbox.checkboxradio()</code>:</p> <pre class="lang-js prettyprint-override"><code>ko.bindingHandlers.checkbox = { init: function(element, valueAccessor) { // set the dom element to a checkbox and initialize it (for jquerymobile) var checkbox = $(element); // let jquerymobile enhance the element checkbox.removeAttr('data-role'); // make it so checkbox.checkboxradio(); // register change event to update the model on changes to the dom checkbox.on('change', function(e) { valueAccessor()(this.checked); }); }, update: function(element, valueAccessor) { // update the checked binding, i.e., check or uncheck the checkbox ko.bindingHandlers.checked.update(element, valueAccessor); // and refresh the element (for jquerymobile) var checkbox = $(element); checkbox.checkboxradio('refresh'); } }; var BoxModel = function(id, name, checked) { var self = this; self.id = ko.observable(id); self.name = ko.observable(name); // checkbox state self.checked = ko.observable(checked); }; function MainViewModel() { var self = this; // hold all added checkboxes self.boxes = ko.observableArray([]); // add new checkbox self.add = function () { var i = $('#newBoxes').find('input[type=checkbox]').length + 1; self.boxes.push(new BoxModel('id'+i, 'name'+i, false)); } } ko.applyBindings(new MainViewModel()); </code></pre> <p>Here is an updated fiddle: <a href="http://jsfiddle.net/USpX5/5/" rel="nofollow">http://jsfiddle.net/USpX5/5/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload