How can I check the duplicate signatures before adding. In the below scenario, I wanted add signature if no signature found the list.
var Signature = function (name, interestDeclared) {
this.Name = ko.observable(name);
this.RelevantInterest = ko.observable(interestDeclared);
}
viewModel = {
signatures: ko.observableArray([]),
addSignature: function () {
var name = $('#signatureName').val();
var intd = $('#interest').is(':checked');
this.signatures.push(new Signature(name, intd));
},
deleteSignature: function (signature) {
this.signatures.remove(signature);
},
insertWitness: function (signature, position) {
this.signatures.splice(position, 0, signature);
}
};
ko.applyBindings(viewModel, document.getElementById("signatories"));
Thanks, -Naren
Using jQuery's grep function:
You could also use some built in utility functions in the KO framework. I've solved this problem with this technique in the past:
I also agree with @madcapnmckay, don't use jQuery to find the values from input, update your model so that
signatureName
andinterest
are bound to your model and use those values.You can use a array utility function like this.
Then update your addSignature method like so.
Here's a great resource for all things js array related.
FYI. You shouldn't be getting the name/interest values via a jquery selector. Instead use KO's value/checked bindings respectively to bind the value to your viewModel. That way your addSignature method can be completely decoupled from the markup.
Hope this helps.