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.
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<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