javascript - Public property bound to button outside of class doesn't have the same value as button bound internally when object is instantiated -


overall, have object keeps track of selected checkbox ids. way pushing/slicing ids into/out of array, $grid.selectedrows. object binds 'refresh' method refresh button. have class created object from, kendogridselection.

my issue button bound inside of class shows correct array values, while button bound outside of class public selectedrows property no longer updates after refresh button clicked.

for testing purposes, have 2 seeselectedrowsarray buttons:

  1. seeselectedrowsarray button (bound internally)
  2. seeselectedrowsarray2 button (bound outside of class)

i testing in chrome version 28.0.1500.95 m

here code:

js

var kendogridselection = function (gridid, pagerselector) {     var $grid = this;     $grid.selectedrows = [];     $grid.gridid = gridid;     $grid.pagerselector = pagerselector;     $grid.grid = $($grid.gridid).data('kendogrid');     $grid.pager = $($grid.pagerselector).data('kendopager');     $grid.gridcheckboxes = $('input[type=checkbox]', $grid.gridid);     $grid.gridrows = $('table tbody tr', $grid.gridid);     $grid.refreshbutton = $('.refreshbutton', $grid.gridid);      $grid.binduievents = function () {         $grid.gridcheckboxes = $('input[type=checkbox]', $grid.gridid);         $grid.gridrows = $('.row', $grid.gridid);          // row click event         /*$($grid.gridrows).click(function (e) {             if (!$(e.target).parent().hasclass('k-hierarchy-cell')) $(this).find('input[type=checkbox]').click();         });*/          // checkbock click event         $($grid.gridcheckboxes).click(function (e) {             console.log('checkbox clicked!');             e.stoppropagation();             var $t = $(this);             var checkboxid = $t.attr('id');             var thisrow = $t.closest('tr');              if ($t.is(':checked')) {                 thisrow.addclass('k-state-selected');                 // add selected[]                 if ($.inarray(checkboxid, $grid.selectedrows) === -1) $grid.selectedrows.push(checkboxid);             } else {                 thisrow.removeclass('k-state-selected');                 // remove selected[]                 $grid.selectedrows.splice($.inarray(checkboxid, $grid.selectedrows), 1);             }         });     }     $grid.gridpersistselected = function () {         $.each($grid.selectedrows, function () {             var $t = $('#' + this);             if ($t) $t.click();         });     }     $grid.pagerchange = function () {         $grid.binduievents();         $grid.gridpersistselected();     }     $grid.refresh = function () {         $grid.selectedrows = [];         $grid.gridcheckboxes.attr('checked', false);         console.log('refresh clicked.');         console.log('$grid.selectedrows: '+$grid.selectedrows);     }      // init     $grid.pagerchange();     // $grid.pager.bind("change", $grid.pagerchange);     // unbind refresh button, rebind     // refresh button     $grid.refreshbutton.click(function(){         console.log('reset!');          $grid.refresh();     });      $('.seeselectedrowsarray').click(function(){         console.log($grid.selectedrows);     });      return {         selectedrows: $grid.selectedrows,         refresh: $grid.refresh,     } }  $(function(){     window.activethreatsgrid = new kendogridselection('.grid', '.pager');    $('.seeselectedrowsarray2').click(function(){         console.log(activethreatsgrid.selectedrows);     }); }); 

html

<div class='grid'>     <div class='row'>         <label><input type="checkbox" id="item1"> </label>     </div>     <div class='row'>         <label><input type="checkbox" id="item2"> </label>     </div>     <div class='row'>         <label><input type="checkbox" id="item3"> </label>     </div>     <div class='row'>         <label><input type="checkbox" id="item4"> </label>     </div>     <div class='row'>         <label><input type="checkbox" id="item5"> </label>     </div>     <div class='pager'>         <input type="button" value="refresh" class="refreshbutton">     </div>         <div><input type="button" value="seeselectedrowsarray" class="seeselectedrowsarray"></div>         <div><input type="button" value="seeselectedrowsarray2" class="seeselectedrowsarray2"></div> </div> 

css

.row{background:blue; height:20px; width:100px; margin-bottom:5px;} 

jsfiddle

demo

what happening:

  • when click on multiple checkboxes, click seeselectedrowsarray, correct values in array. can many times , still correct values.
  • when hit refresh button, console.logs tell me selectedrows array empty. when click seeselectedrowsarray, array empty (expected). when click seeselectedrowsarray2, array still has values in it.

update 1

what have found if bind $grid.selectedrows button click within class, gets current values, after refresh. if bind public selectedrows button click outside of class, after refresh button clicked, selectedrows no longer updates , gets stuck @ value before refresh.

why button bound internally show correct array values, while button bound outside of class public selectedrows property no longer updates after refresh button clicked?

any appreciated!

your problem lies in how returning/tracking references object.

in constructor, set $grid = this

however, return new object result of function:

return {     selectedrows: $grid.selectedrows,     refresh: $grid.refresh, } 

that returned object holds reference current value of $grid.selectedrows

when refresh method sets $grid.selectedrows new array breaks associated value returned object remains set original array.

change refresh from:

$grid.selectedrows = [] 

to:

$grid.selectedrows.length = 0; 

demo


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -