0

After update i need to replace a item in the ko array. The problem is with the replace function i need to have the old item.

Here is my function:

self.saveMarket = function (market) {
    var item = ko.toJS(market);

    $.ajax({
        type: (item.MarketId != "" ? 'PUT' : 'POST'),
        url: url + (item.MarketId != "" ? '?id=' + item.MarketId : ''),
        data: item,
        success: function (data) {
            self.Markets.push(market);
            self.showList();
        },
        error: function (err) {
            alert(err);
        },
    });
}

This should handle both insert and update. The insert works with the push(market), but if i do an update, how can i replace the old item with the new?

Thanks in advice

1 Answer 1

1

You may have to iterate through your observable array (using ko.utils.arrayForEach or $.each), find the item with the matching marketid, remove that element from array, then push the new one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.