0

I am studying the Ext JS framework and faced the following difficulty of sending a request through a proxy in the store file.For some reason, the request does not go away, but there is no error either. Also, Maybe somewhere in the code below I made a mistake. I don't particularly understand how to get data from the store from other components, that is, for example, I get data in the FormViewStore, and I need to get them, for example, in the NavView component, can I do it somehow, if so, please tell me how?

Below is the FormView file

Ext.define("ModernApp.view.form.FormView", {
  extend: "Ext.form.Panel",
  xtype: "formview",
  title: "Custom Form",
  store: { type: "formviewstore" },
  controller: "formviewcontroller",
  cls: "formView",
  buttons: [
    {
      text: "Оправить",
      handler: "onSubmit",
    },
    {
      text: "Отмена",
      handler: "onCancel",
    },
  ],
  listeners: {
    afterrender: "onFormRender",
  },

  onFormRender() {
    console.log("onFormRender");
    var controller = this.getController();

    // Загрузка данных из сторы
    controller.loadFormViewStore();
  },
  items: [
    {
      xtype: "fieldset",
      title: "About You",
      instructions: "Tell us all about yourself",
      items: [
        {
          xtype: "textfield",
          name: "firstName",
          label: "First Name",
          listeners: {
            change: {
              fn: function (event) {
                const name = Ext.ComponentQuery.query(
                  "textfield[name=firstName]"
                )[0].getValue();

                const surname = Ext.ComponentQuery.query(
                  "textfield[name=lastName]"
                )[0].setValue(name || "");
              },
            },
          },
        },
        {
          xtype: "textfield",
          name: "lastName",
          label: "Last Name",
        },
        {
          xtype: "datefield",
          name: "birthday",
          label: "Birthday",
        },
        {
          xtype: "emailfield",
          name: "email",
          label: "Email",
        },
        {
          xtype: "passwordfield",
          name: "password",
          label: "Password",
        },
      ],
    },
  ],
});

Below is the FormViewStore file

Ext.define("ModerdApp.view.form.FormViewStore", {
  extend: "Ext.data.Store",
  alias: "formviewstore",
  proxy: {
    type: "ajax",
    url: "https://jsonplaceholder.typicode.com/posts",
    reader: {
      type: "myreader",
    },
  },
});

is the FormViewStore file

Ext.define("ModernApp.view.form.reader.Json", {
  extend: "Ext.data.reader.Json",
  alias: "myreader",
  getResponseData: function (responce) {
    console.log("response", responce);
  },
});

I looked at the spread in the documentation, but I did not find a specific example of how to make a request through a proxy and get data in a customReader. Then I went to StackOverflow in the questions section but didn't find an answer to my question. Next I tried to change the proxy configuration but it didn't help either

  • It is not easy to tell it without the complete code, but one thing is sure: your `alias` definitions are incorrect. You have to prefix the aliases in order to work with `type: "myreader"` etc. The syntax for the reader is: `alias: "reader.myreader"`, for the store: `alias: "store.formviewstore"`. – Peter Koltai May 30 '23 at 20:48
  • thanks are lot! I corrected the alias, but it didn't help, do I understand correctly that in order for the data in the store file to start loading when initializing the FormView component, you need to add the following method in the FormView file onFormRender() { console.log("onFormRender"); var controller = this.getController(); // Загрузка данных из сторы controller.loadFormViewStore(); }, @Peter Koltai – Евгений Мушков May 31 '23 at 07:25
  • Either you load the store with `.load()` or `.reload()` method, or the framework can do it automatically. For example if you configure a store with `autoLoad: true` and you assign it to a grid it will be loaded. – Peter Koltai May 31 '23 at 07:32
  • If you can create a Sencha Fiddle to demonstrate the problem I can have a look at it. – Peter Koltai May 31 '23 at 07:33
  • Of course! https://fiddle.sencha.com/#fiddle/3o38&view/editor Here is a link to my Fiddle, If you suddenly manage to send a request, then you may be able to suggest how to use customReader correctly, in order for the data that comes from the server to be processed in some way? @Peter Koltai – Евгений Мушков May 31 '23 at 09:01

1 Answers1

1

By checking your fiddle the problem is that you are trying to set the store for the form panel in FormView.js like:

store: { type: "formviewstore" },

But look at the documentation here: Ext.form.Panel does not have a config named store. This could work with a grid for example which does. I don't know what do you want to use this store for but you have to load it manually. (By the way a store is a collection of models, and forms usually display one model, not a list of models.)

But your store and proxy works, you can easily try it. For example add this code to the one of your button's handler (you can add it anywhere just to try out):

 buttons: [
    {
      text: "Оправить",
      handler: function() {
          // create a store
          const store = Ext.create('ModerdApp.view.form.FormViewStore');
          // load it
          store.load();
      },
    },
    {
      text: "Отмена",
      handler: "onCancel",
    },
  ],

Now if you press the button and check for console, after a short while you will see the output of console.log in FormViewReader.js, from getResponsData.

About the reader: it is pretty simple, it takes the response as input and has to return whatever you'd like to transform it. Just stop the debugger there and explore the structure of response and find out how you'd like to return it.

The following simple example assumes that the response had the result in responseText, you want do decode it and return the results and whether if was a success of error:

getResponseData: function (response) {
    var json =  Ext.JSON.decode(response.request.result.responseText);
    if (response.status==200) {
        json = {
            "success": "true",
            "results": json
        }
    }
    else {
        json = {
            "success": "false",
            "errors": json
        };
    }
    return json;
}
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
  • Many thanks for such a detailed answer, now everything has become very clear. The documentation, of course, is very large, so you can’t immediately focus on what to pay attention to. @Peter Koltai – Евгений Мушков Jun 02 '23 at 13:55
  • You are welcome. Ext JS has a steep learning curve for sure. – Peter Koltai Jun 02 '23 at 14:49
  • Peter, hello! To my shame, I have encountered new difficulties ((If you have free time, could you look at my Fiddle. I created a question, but unfortunately no one answered me ((Here is the link, I will be very grateful to you https://stackoverflow.com/questions/76405860/error-in-sencha-fiddle-unrecognized-alias-some-requested-files-failed-to-load @Peter Koltai – Евгений Мушков Jun 05 '23 at 14:15