Module Help - Native Javascript (ES6)

Asked

Viewed 45 times

-3

I need a simple help with javascript modules, I am studying and I have many questions.

In my example, I charge Fetch.js, which is a unique file that I use to make requests, GET, PUT, POST e DELETE, and everything is going well so far.

So I have a file AppController.js, where I get some data from the application, such as the name of the application, for example.

Appcontroller.js

import Fetch from './Fetch.js';

// GET
async function getApplication() {
    return await Fetch.get('/application');
}

export default getApplication;

So I have another file Index.js, where do I matter AppController.js

Index.js

import InfoApplication from './AppController.js';

let ObjectApplication = {};

const initApplication = () => {
    InfoApplication();
}

initApplication();

Fetch js.

// Fetch.js
const _apiHost = 'http://banco_de_dados';

async function request(url, params, method = 'GET') {

  const options = {
    method,
    headers: {
      'Content-Type': 'application/json'
    }
  };

  if (params) {   
    if (method === 'GET') {
      url += '?' + objectToQueryString(params);
    } else {        
      options.body = JSON.stringify(params);      
    }
  }

  const response = await fetch(_apiHost + url, options);

  if (response.status !== 200) {
    return generateErrorResponse('The server responded with an unexpected status.');
  }

  const result = await response.json();

  return result;

}

function objectToQueryString(obj) {
  return Object.keys(obj).map(key => key + '=' + obj[key]).join('&'); 
}

function generateErrorResponse(message) {
  return {
    status : 'error',
    message
  };
}

function get(url, params) {   
  return request(url, params);
}

function create(url, params) {    
  return request(url, params, 'POST');
}

 function update(url, params) {
  return request(url, params, 'PUT');
}

function remove(url, params) {   
  return request(url, params, 'DELETE');
}

export default {
  get,
  create,
  update,
  remove
};

And here begins my doubt:

Question:

I want to store the request return on a global object, so I can access the properties of that object anywhere, it’s possible?

For example:

ObjectApplication.name

  • if you are only using nodejs or some js framework?

  • I did not find the IIFE quoted question. I would not be trying to do something like this (function () {InfoApplication()})(), even if it means the same as only InfoApplication()

  • @Virgilionovic, I’m not using nodejs and I’m not using any webpack or framework. It’s native JS!

  • 1

    It’s wrong here: getApplication() because of the inner part and lack so much that your question is bad, has no foundation, has no way to answer and did not say to what came ...

  • @Augustovasques, it helps me about question 1, but I have the second question that for me is more complex to understand, I thought about singletons, but I can’t imagine how to use this module.

  • do not ask two questions in one. It is unpleasant to answer and is contrary to the rules of the site.

  • Where are you doing it ... since you don’t use nodejs?

  • I am doing in pure js, only created the files in . js and loading an HTML file like this: <script type="module" src"./index.js"></scritp>

  • and there is no error on the screen, because it should?

  • You’re not making any mistakes! I just want to know if it is possible to store the return of the request in an object and access this object anywhere in the application

  • 1

    Yes it is possible.

  • may not be wrong, but for example your getApplication() does nothing he just has a const with call on a Fetch that we do not know the implementation and if the request can be made with fetch ... I don’t know I think your code has several problems.

  • is possible but not with that code...

  • @Virgilionovic, the getApplication() makes a request, see that at the beginning of the file, I also import another Fetch.js file, and in this Fetch, I have the get method

  • That one getApplication() gives you a const with a promisse, but, this is not solved, IE, we do not know how it was implemented and we have no way to guess. or puts all the code or rephrases the question and it seems that your doubt is up to another.

  • @Virgilionovic, I put all the code

  • your getApplication() needs only one return then async function getApplication() {&#xA; return await Fetch.get('/application');&#xA;}

  • 1

    And what difficulty did you find to store the request return in a global variable?

  • @Virgilionovic, great, also worked with return, but I want this return to stay in this object ObjectApplication

  • @v.Saints my difficulty is how to store the return of the request in the objectApplication

Show 15 more comments

1 answer

2

let ObjectApplication = {};

Choose an option

window.ObjectApplication = null;
globalThis.ObjectApplication = null;
this.ObjectApplication = null;
(0, eval)('this').ObjectApplication = null;

So

InfoApplication().then(app => ObjectApplication = app);
  • It’s not right, I want to assign the return of the request to this object, and not set an empty object.

  • @Wagnerfillio, window.ObjectApplication = InfoApplication()?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.