0

so I'm fairly new to "classes" in Javascript and how they work, and have been given a piece of code that uses it in a very strict way. So, basically for the project I'm working on, there's a overall class, Combination, and then a view and a service class created inside of combination. Now, I have to load Service before I load View because I need access to Services functions for the View.addListeners function. My question is if i'm getting data from ajax and then parsing that data in service, how do I then pass that data into the view to be used in a function in the view?

Example Code (very shorthand):

class Combination {
  constructor(someData) {
    this.Service = new CombinationService(this);
    this.View = new CombinationView(this, someData);
  }
}

class CombinationView {
  constructor(parent,lists) {
    // Initialization
    this.functionUsingData(getData);
     }

    functionUsingData(getData) {
      $("apples").dxDataGrid
    }
  }

class CombinationService {
    constructor(parent) {
    this.parent = parent;
  }
  functionGettingData() {
    $.ajax({
      method: "GET"
      url: somewhere/somewhereElse
      data: { data }
    }).then(function (data) {
      try {
        let getData = JSON.parse(data);
        return getData
      } catch (exception) {
    }
  });

The problem that I'm getting is that if I try to add something like this.parent.View.functionUsingData(getData) in service, this.parent is undefined because Service is loaded before view, however if I try passing the data in View, like let getData = this.Service.functionGettingData(); then because of JS' asynchronous nature, it doesn't wait for the data to load. HELP!

  • The code above will not even run. You are missing closing braces `}` and have an extra closing `)` at the end. Can't help much if I can't tell what you are trying to do due to incorrect formatting. Did you intend to define CombinationService inside of CombinationView? – Doug Coburn Jul 18 '18 at 15:54
  • @DougCoburn Fixed – WebDev1111110 Jul 18 '18 at 15:59

0 Answers0