Vineesh TP
Vineesh TP

Reputation: 7973

How to check Object existence in ListModel

I want to add object dynamically to the ListModel. But I need to append the object only if it does not exist in the model.

I gone through the Documentation I couldn't find any methods for this. Is there any other ways to check this

Upvotes: 0

Views: 1829

Answers (1)

user3407729
user3407729

Reputation: 11

The ListModel method get(index) enables you to access each element. It also has a property count, which tells you how many elements are in it. So, something like this:

function appendIfNotExist(objectToAppend) {
    for (var i = 0; i < myListModel.count; i++) {
        if (myListModel.get(i) == objectToAppend) {
            return
        }
    }
    ListModel.append(objectToAppend)
}

Upvotes: 0

Related Questions