Reputation: 4739
I'm not sure that makes sense. Not sure how to word it.
Basically I have this.
function Line(id, name, equipType, model, height, length, loadsWeek, weight, width, pallets) {
this.id = ko.observable(id);
this.name = ko.observable(name);
this.height = ko.observable(height);
this.length = ko.observable(length);
this.weight = ko.observable(weight);
this.width = ko.observable(width);
this.model = ko.observable(model);
this.equipType = ko.observable(equipType);
this.loadsWeek = ko.observable(loadsWeek);
this.perimeter = ko.dependentObservable(function() {
return parseInt(this.height()) + parseInt(this.length())
}, this);
var mapped = ko.utils.arrayMap(pallets, function(pallet) {
return new Pallet(pallet.id, pallet.name, pallet.height, pallet.width, this)
});
this.pallets = ko.observableArray(mapped);
}
function Pallet(id, name, height, width, line) {
this.id = ko.observable(id);
this.name = ko.observable(name);
this.height = ko.observable(height);
this.width = ko.observable(width);
this.line = ko.dependentObservable(line);
//calculate something here that uses a variable from Line
}
Now in the Pallet I need to access the line object to get the values from it. I'm going to do calculations that requires the values from the Line object. This code doesn't work right now because I apparently can't pass "this"(the line) into the Pallet object.
Here's the rest of the code
var viewModel = {
lines: ko.observableArray([]),
activeTab: ko.observable()
};
$.getJSON("/json/all/lines", { customer_id : customer_id } , function(data) {
//ko.mapping.fromJS(data, null, viewModel.lines);
var mappedData = ko.utils.arrayMap(data, function(line) {
return new Line(line.id, line.name, line.equipType, line.model, line.height, line.length, line.loadsWeek, line.weight, line.width, line.pallets)
});
});
Upvotes: 2
Views: 801
Reputation: 16018
The problem is probably that this
within the anonymous function you are passing to arrayMap is not the same this
as you think.
Try something like :
var that = this;
var mapped = ko.utils.arrayMap(pallets, function(pallet) {
return new Pallet(pallet.id, pallet.name, pallet.height, pallet.width, that)
});
------Then
function Pallet(id, name, height, width, line) {
this.id = ko.observable(id);
this.name = ko.observable(name);
this.height = ko.observable(height);
this.width = ko.observable(width);
this.line = ko.dependentObservable(line);
//calculate something here that uses a variable from Line
this.something = ko.observable(function() {
return this.line().somethingFromParent();
});
}
Upvotes: 5