How do I switch a result value based on a condition?

49 views
Skip to first unread message

Helene Francke

unread,
Dec 9, 2016, 8:07:40 AM12/9/16
to KnockoutJS
Hi!

I'm trying to change a resource collected through an ajax call based on the status for that particular resource. The data I receive is an array of JSON-objects and in each object there is a property populated with a link to an image. Although the link is always there, the image it links to isn't. So what I would like to do in my success call is something like this:

function viewModel () {
    
    this.employeeList = ko.observableArray([]);

    //ajax call......



    success: function (data) {
                    
                    var mappedEmpl = $.map(data, function (item) {
                        return {
                            Name: item.Name,
                            Position: item.Position,
                            Department: item.Department,
                            ShortNumber: item.ShortNumber,
                            Mail: item.MailAddress,
                            RoomNo: item.RoomNo,
                            EmployeeNo: item.EmployeeNo,
                            image: item.ImageUrl

                           if (image.status === 404) {
                                    //set src-attribute to a default image
                          }

                        }
                    });

                    this.employeeList(mappedEmpl);
                },
}
ko.applyBindings(new EmployeeDataModel());


Is there an easy way of doing this, or am I going to have to split them up into seperate ajax calls? I already tried to use onerror, but that apparently targeted ALL the images regardless if it existed or not.

Thanks!
Reply all
Reply to author
Forward
0 new messages