jquerydatatablesdurandalknockout-2.0durandal-2.0

TypeError cyclic object value when trying to change a ko.observable from an jquery trigger inside jquery DataTables


I'm trying to change a ko.observable value when clicking on a jquery.DataTables table-row to the id of that row. I'm instantiating the DataTable in durandal's attached function after I fetched data from the server in the activate cycle. Even without any on-click logic, I can't get it to change the observable value after I instantiated the Datatable.

code below:

self.attached = function(view, parent) {
    self.selectedCategory().schedule_id(1337);
    self.dt = $('#reservation_schedule_table').DataTable();
}

works fine. But, when I write it like this,

self.attached = function(view, parent) {
    self.dt = $('#reservation_schedule_table').DataTable();
    self.selectedCategory().schedule_id(1337);
}

I get a cyclic object error.

TypeError: cyclic object value
return JSON.stringify(ko.utils.unwrapObservable(data), replacer, space);
knockou...7981312 (line 537, col 19)

Can somebody tell me why that is and how to prevent this from happening?


Solution

  • I think I got it. Because the only thing that's being returned from the module is a new Instance of the viewModel itself, the DataTable instance had to be relative to the view-parameter of the attached function. Haven't fully understood why this is, yet. But I'm going to read up on it. For starters, this is how the code should look like:

    self.attached = function(view, parent) {
        view.dt = $('#reservation_schedule_table').DataTable();
        self.selectedCategory().schedule_id(1337);
    }
    

    And wherever the Datatable is used inside one of the lifecycle functions, it has to be as view.dt