Cómo acceder a un objeto desde otro objeto observable

I'm not sure that makes sense. Not sure how to word it.

Básicamente tengo esto.

    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)        
        });

});

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

El problema es probablemente que this within the anonymous function you are passing to arrayMap is not the same this Como se puede pensar.

Prueba algo como:

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();
    });
  }  

respondido 08 nov., 11:19

Thanks. Mismanaging this. I thought I was out of that phase. Haha. - Dibujó H

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.