javascript - Adding function into JSON object -


i have json object; let’s call model; has 2 properties of type array. model has function “hasanyfilterselected” checks if of array property populated.i have search button & 2 drop downlist on ui. when user clicks on search button, there steps occurs:

1>push selected values drop down list model’s array properties.

2>model.hasanyfilterselected() checks if of array has values, in debugger see model populated values.

3>if yes, refresh kendo grid executing datasource.read()

4>kendo grid calls “getfilters” method returns model, again in debugger see model populated values.

5>then somehow hasanyfilterselected() gets called again , time model array properties undefined. looks kendo trying serialize model again , time model has different instance im not sure.

$(function () {      var model = {         selectedtaxyears: [],          selectedstates: [],          hasanyfilterselected: function () {             return !(this.selectedtaxyears.length == 0 &&                                  this.selectedstates.length == 0)         }     };          $_btnsearch.click(function () {         pushfilters();         if (model.hasanyfilterselected()) // returns true when array populated. in debugger see populated         {             $(gridid).data("kendogrid").datasource.read();              // fails @ above line when datasource reads             // calls getfilters method expected, see model populated.             // calls hasanyfilterselected() function again, ( don’t know why) , @ time selectedtaxyears & selectedstates undefined, fails             // in debugger not see model populated. looks different instance of model              $(gridid).data('kendogrid').refresh();         }        });       function pushfilters() {       model.selectedtaxyears = $_taxyears.val();        model.selectedstates = $_stateprovience.val();            }          function getfilters() {             return model;         }      var datasource = $_grid.data("kendogrid").datasource;     datasource.transport.options.read.data = getfilters; }) 

i know why happening? defining json function properly? preferred approach?

if understand properly, values retained, function being overwritten or ignored somehow? (probably when being copied somewhere, point out.) don't know if first step work, possibly try writing more definitive class prototype function:

var model_class = function() {     this.selectedtaxyears = [];      this.selectedstates = []; } model_class.prototype = {     hasanyfilterselected: function () {         return !(this.selectedtaxyears.length == 0 &&                              this.selectedstates.length == 0) } var model = new model_class(); 

if gets overwritten, alternative define function elsewhere in singleton library or on 'window' scope, , call using '.apply':

if ( model_hasanyfilterselected.apply( ) ) {     //// etc } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -