user1167777
user1167777

Reputation: 93

Add property to ko observable array items

I have an observable array that contains objects. I want to add a "editable" property to the array after the data call. I think I just need to loop through the items and add an item.editable=false to each one but I am not sure where I would need to do this.

this is the function my viewmodel

function GetemployeeDetails() {
    return employeeDataService.getEmpDetails(employeeDetails);
    }

Dataservice calls a function that does a ajax post.

var dataservice = {

        getEmpDetails: getEmpDetails
    };

function that populates from data

var getempDetails = function(employeeDetailsObservable) {
        var dataObservableArray = ko.observableArray([]);
        var newJson;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/api/employee/employeeDetailsByID/",
            data: '{}',
            async: false,
            success: function(dataIn) {
                newJson = $.parseJSON(dataIn);
            employeeDetailsObservable([]);
           newJson.forEach(function(e) {
                    var empdetails=new  emdetailsmodel(e.name,e.number)
                    employeeDetailsObservable(empdetails);
                });

Upvotes: 0

Views: 2698

Answers (1)

Marlon Bernardes
Marlon Bernardes

Reputation: 13853

Just add editable = false (or editable = ko.observable(false)) inside your forEach:

newJson.forEach(function(e) {
  var empdetails=new  emdetailsmodel(e.name,e.number)
  empdetails.editable = false;
  employeeDetailsObservable(empdetails);
});

Upvotes: 2

Related Questions