0

I'am using Knockout.js. I have a function like this:

function deviceGroupItem(item) {
    this.DeviceGroupName = item.DeviceGroupName;
    this.groupDevicesVisible = ko.observable(false)
    this.groupDevicesArray = ko.observableArray();
    this.deviceGroupClick = function () {
        if (this.groupDevicesVisible() == false) {
            this.groupDevicesVisible(true)
            $.ajax({
                url: returnServer() + '/api/Mobile/getRoomDevices?accessToken=' + localStorage.getItem('Token'),
                type: "GET",
                dataType: "json",
                success: function (data) {
                    this.groupDevicesArray()($.map(data, function (item) {
                        return new roomDeviceItem(item);
                    }))
                },
                error: function () {

                }
            })
        } else {
            this.groupDevicesVisible(false)
        }
    }
    return this;
}

Problem is, when I'am trying bind:

this.groupDevicesArray = ko.observableArray();

Using:

this.groupDevicesArray()($.map(data, function (item) {
                        return new roomDeviceItem(item);
                    }))

I'am receiving error "this.groupDevicesArray is not a function". Honestly, I dont know how to do this in correct way. Do You know how can I achieve that?

2
  • Try this : this.groupDevicesArray (ko.utils.arrayMap(data, function(item) { return new roomDeviceItem(item); }); or this.groupDevicesArray($.map(data, function (item) { return new roomDeviceItem(item); })) Commented Sep 4, 2015 at 13:04
  • Thank You for answer, but still the same. Commented Sep 4, 2015 at 13:12

2 Answers 2

1

The issue is because of you referring observable Array with this inside the function deviceGroupClick which does not exist because this refers to current context .

This technique depends on current closure which is a pseudo variable that may differ from scope to scope dynamically .

viewModel:

function roomDeviceItem(data) {
    this.room = ko.observable(data.room)
}

function deviceGroupItem() {
    var self=this; //Assign this to self & use self everywhere
    self.groupDevicesArray = ko.observableArray();
    self.deviceGroupClick = function () {
        $.ajax({
            url: '/echo/json/',
            type: "GET",
            dataType: "json",
            success: function (data) {
                data = [{
                    'room': 'One'
                }, {
                    'room': 'Two'
                }]
                self.groupDevicesArray($.map(data, function (item) {
                    return new roomDeviceItem(item);
                }))
            }
        });
    };
};
ko.applyBindings(new deviceGroupItem());

working sample here

Just in-case if you are looking for solution with this you need to use bind(this) to get reference of outer closure check here

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

1 Comment

I found a solution, but this also works, so thank You for help!
0

Try

this.groupDevicesArray($.map(data, function (item) {
  return new roomDeviceItem(item);
}));

groupDevicesArray is observableArray and $.map returns an array.

2 Comments

Thank You for answer, but problem is still the same.
jsfiddle would help then.

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.