ui5-community / generator-ui5-project

Generator for UI5-based web-apps which use the official UI5 tooling and support multiple deployment targets such as the SAP Business Technology Platform
Apache License 2.0
44 stars 27 forks source link

ajax and xhr are not available in #63

Closed horsemanjackyliu closed 1 year ago

horsemanjackyliu commented 1 year ago

Easy-Ui5 version:

Run yo easy-ui5 --plugins to print all relevant version strings

Node.js: v14.19.3 yeoman-environment: 3.9.1 generator-easy-ui5: 3.3.0 home: C:\Develop\nodejs\node_global\node_modules\generator-easy-ui5 pluginsHome: C:\Develop\nodejs\node_global\node_modules\generator-easy-ui5\plugin-generators

Available generators:

OS/version: win 10 21H2

Browser/version (+device/version): Microsoft Edge 107.0.1418.62 (Official build) (64-bit)

Steps to reproduce the problem:

  1. Step 1 use yo easy-ui5 project to generate a ui5 project.
  2. Step 2 Change MainView.controller.js
  3. Step 3 add the following code in onInit funnction: let xhr = new XMLHttpRequest();

the error is "Cannot find name 'XMLHttpRequest'.ts(2304)"

$.ajax also can not be used .

What is the expected result?

In the generated project XHR and ajax can be used .

What happens instead?

Any other information? (attach screenshot if possible)

nicoschoenteich commented 1 year ago

Hi @horsemanjackyliu,

I just tested it myself and this works:

MainView.controller.js

sap.ui.define(
    ["./BaseController"],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller) {
        "use strict";

        return Controller.extend("com.myorg.myUI5App.controller.MainView", {
            onInit: function () {
                console.log(jQuery.ajax())
                console.log(new XMLHttpRequest())
            },
        });
    }
);
Screenshot 2022-12-06 at 17 12 48

Can you try jQuery.ajax() ?

Best, Nico

wridgeu commented 1 year ago

Hey Nico,

hope you don't mind if I add a cross reference to the SAP Community question: https://blogs.sap.com/2021/04/09/easy-ui5-3.0-from-community-contributions-to-community-plugins/comment-page-1/#comment-650545

Some comments: The way you access jQuery here is by using the global namespace (tbh. kind of like with the XHR anyway :D) but if you really want to use jQuery within UI5 atm. shouldn't you also import it within sap.ui.define (using sap/ui/thirdparty/jquery) rather than just accessing the global?

Regarding the XMLHttpRequest error, I think this relates to missing TS types maybe?

BR, Marco :)

nicoschoenteich commented 1 year ago

Hi @wridgeu, Thanks, appreciate it! Makes total sense to skip the whole global thing - thanks for pointing it out.

@horsemanjackyliu, do you experience runtime errors as well? Or is this only regarding type checking during development?

Best, Nico

wridgeu commented 1 year ago

Hey @nicoschoenteich,

I think this issue can be closed. Dont want to be too presumptuous here but it doesn't seem to have been an actual error and I think it was mostly for his blog post, which seems to have worked out fine. :)

BR, Marco