I had an object that I created without a constructor. there was no need for it (it was used in one copy). In the process of writing code, exactly the same objects appeared and in order to avoid dubbing of the code, I decided to create a constructor for my "initial" object.

Here is the code for my initial object:

var documents = { data : [], btnTitle : "ADD ALL", numberOfSelected: 0, addOrRemoveAll : function() { var status = documents.numberOfSelected < documents.data.length; for (var i = 0; i < documents.data.length; i++) { if(status && documents.data[i].checked == false) { documents.data[i].checked = true; documents.numberOfSelected += 1; attachedLinks.addData(documents.data[i]); } if(!status && documents.data[i].checked == true){ documents.data[i].checked = false; documents.numberOfSelected -= 1; for (var key in attachedLinks.data) { if(attachedLinks.data[key].kindOf == 0){ attachedLinks.data.splice(key, 1); } } } } documents.updateTitle(); }, updateNumberOfSelected: function(event, element){ if(documents.data[element.index].checked){ documents.numberOfSelected += 1; attachedLinks.addData(documents.data[element.index]); } else{ documents.numberOfSelected -= 1; for (var key in attachedLinks.data) { if(attachedLinks.data[key].id == documents.data[element.index].id && attachedLinks.data[key].kindOf == 0){ attachedLinks.data.splice(key, 1); } } } documents.updateTitle(); }, updateTitle: function(){ if(documents.numberOfSelected < documents.data.length){ documents.btnTitle = "ADD ALL"; } else{ documents.btnTitle = "REMOVE ALL"; } attachedLinks.update(); } }; 

Such a constructor I wrote:

 function DataList(data){ this.data = data; this.btnTitle = "ADD ALL"; this.numberOfSelected = 0; this.addOrRemoveAll = function() { var status = this.numberOfSelected < this.data.length; for (var i = 0; i < this.data.length; i++) { if(status && this.data[i].checked == false) { this.data[i].checked = true; this.numberOfSelected += 1; attachedLinks.addData(this.data[i]); } if(!status && this.data[i].checked == true){ this.data[i].checked = false; this.numberOfSelected -= 1; for (var key in attachedLinks.data) { if(attachedLinks.data[key].kindOf == 0){ attachedLinks.data.splice(key, 1); } } } } this.updateTitle(); }; this.updateNumberOfSelected = function(event, element){ if(this.data[element.index].checked){ this.numberOfSelected += 1; attachedLinks.addData(this.data[element.index]); } else{ this.numberOfSelected -= 1; for (var key in attachedLinks.data) { if(attachedLinks.data[key].id == this.data[element.index].id && attachedLinks.data[key].kindOf == 0){ attachedLinks.data.splice(key, 1); } } } this.updateTitle(); }; this.updateTitle = function(){ if(this.numberOfSelected < this.data.length){ this.btnTitle = "ADD ALL"; } else{ this.btnTitle = "REMOVE ALL"; } attachedLinks.update(); }; } 

and so created an instance of the object:

 documents = new DataList(JSON.parse(loaded.response)["documents"]); 

in consequence of which errors appeared:

"Uncaught TypeError: Cannot read property 'length' of undefined"

in the var status = this.numberOfSelected < this.data.length; line var status = this.numberOfSelected < this.data.length; and

"Uncaught TypeError: Cannot read property '0' of undefined"

in the if(this.data[element.index].checked) line if(this.data[element.index].checked) well, and if you “try to fix” these moments, then there are many more “similar” elements in the constructor.

I wrote a lot and now it's time for a question, please tell me how to write the correct constructor, I suppose I incorrectly use "this.", But after reading the theory a couple of times, I did not understand why.

RivetsJS - official website (eng)
RivetsJS - article on Habré with Russian adaptation

Thanks for attention.

  • one
    JSON.parse (loaded.response) ["documents"] returns undefined builds to check which object is being received as a result of JSON.parse (loaded.response) - Grundy
  • @Grundy tried to create an array of objects "manually", the result is the same. If you do not use the constructor, everything works (but there dubbing is a huge one) because I describe three identical objects. Constructor for the test: function DataTest(kindOf, checked, title) { this.kindOf = kindOf; this.checked = checked; this.title = "Awesome book " + title; } function DataTest(kindOf, checked, title) { this.kindOf = kindOf; this.checked = checked; this.title = "Awesome book " + title; } function DataTest(kindOf, checked, title) { this.kindOf = kindOf; this.checked = checked; this.title = "Awesome book " + title; } Create: var data = []; for(var i = 0; i < 10; i++){ data[i] = new DataTest(0, false, "Cool Author"); } var data = []; for(var i = 0; i < 10; i++){ data[i] = new DataTest(0, false, "Cool Author"); } var data = []; for(var i = 0; i < 10; i++){ data[i] = new DataTest(0, false, "Cool Author"); } Create last object: documents = new DataList(data); . - Phobophobia
  • one
    make a minimal reproducible example . Without it, you can only say that you pass undefined and get the corresponding error - Grundy
  • @grundy figured out a bit about the "problem", it's all about visibility. The method that I described in the constructor is invoked using rivets when clicking on a checkBox, after calling the method, all data from the class that was described above is no longer available. <li rv-each-item="documents.data"> <label> <input type="checkbox" rv-checked="item.checked" rv-on-change="documents.updateNumberOfSelected"/> </label> </li> Can I somehow get this method to “see” the data I need, which is described in the same constructor? - Phobophobia
  • one
    Is it possible to somehow make this method “see” the data I need, which are described in the same constructor? - Without a minimal reproducible example, nothing can be said. While it is not clear what the rivets and how it is used - Grundy

0