Use unshift()
to add something to the beginning of an array.
self.addContact = function() {
self.contacts.unshift({
firstName: "",
fathersLast: "",
country: ""
});
};
You can use $index
to refer to the current index of the array that's being iterated over (it's an observable, so you need to add the parenthesis).
<h4 data-bind="text: 'Visitor ' + ($index() + 1)"></h4>
Finally, you can prevent removal of the last contact with a simple if
statement checking the length (it may be prudent to add an alert
otherwise):
self.removeContact = function(contact) {
if (self.contacts.length > 1) {
self.contacts.remove(contact);
}
};