How to use api property in store proxy configuration - CRUD methods

Can someone tell me where we should write the code for create / update etc. I am writing it under the proxy api property. How to check the conclusion. Please tell me how to use the api store.

I am testing, so please be more clear to understand and use the functionality

+4
source share
1 answer

The configuration itself is described here in the docs. But it looks like you have the correct syntax.

The first thing you should know is that this configuration only applies to proxies that extend Ext.data.proxy.Server . Read the "Proxy Types" section here .

By defining different URLs in this configuration, you simply indicate in the repository where to send the ajax request to perform various CRUD actions on your server side.

For example, calling store.load() will send an ajax request to api.read URL, you need to make sure that this URL returns the data correctly.

An Ext.data.Store internally tracks other actions performed on dirty records (created, updated, or destroyed). Based on this, it will send an ajax request to the corresponding api configuration url. Or if you performed various types of actions, for example. created and deleted entries, it will send more than one ajax request (one for each URL) along with data about the entries you created or deleted.

Here is an example code to illustrate (which you can also use for testing if you fill in your own URLs and data.model ). In this example, the default reader / writer is used, which sends data to the server as JSON (the proxy server has settings for specifying a different format).

 var myStore = Ext.create('Ext.data.Store', { model: 'MyApp.model.SomeModel', proxy: { type: 'ajax', // without api defined ALL ajax calls will use the 'url' config url: '/some/url', api: { create: '/some/url/to/insert/records/in/db', read: '/some/url/to/select/records/from/db', update: '/some/url/to/update/records/in/db', destroy: '/some/url/to/delete/records/in/db' } } } // this calls the api.read URL myStore.load(); // assuming we now have records, this will delete the first record // on the client side (it will error if there are not records) myStore.remove(myStore.first()); // the store knows we deleted a record so this will call the api.destroy URL myStore.sync(); // this updates the first record on the client side myStore.first().set('some_field_name', 'a string value'); // now we are creating a record on the client side myStore.add(Ext.create('MyApp.model.SomeModel')); // the store knows we updated AND created a record so this will call the // api.update URL AND the api.create URL myStore.sync(); 

Two other useful information about this:

  • There is a proxy configuration called batchActions , described here in the docs . By default, this is true , which means that all CRUD actions of a certain type are grouped into an array when sending a request to the database. For instance. if you deleted 4 entries, api.destroy url will not receive 4 ajax requests, it will receive 1 ajax with an array of 4 entries. This helps reduce network traffic while you configure your URL to process the array. You can set this configuration to false , and the store will send 4 instead requesting the api.destroy URL. You can also set the allowSingle config writer ( described here ) to ensure that all requests are sent as an array, even if there is only one entry (this way you can always configure your server-side code to process the array).

  • Ext.data.Store configured to handle a callback to create and update actions, you just need to make sure your URL is sent back. When you call myStore.sync() , the store will automatically replace the client-side entry with the one you send in your Chime. This is useful for the created records, because you can send back the correct database identifier with the created record and get it available on the client side, later if the user wants to change or delete the record with which you have the correct database identifier. You can also do other processing on the server side and send back other data so that you have a complete record (for example, I sometimes send back to create a user ID and create time).

+16
source

Source: https://habr.com/ru/post/1444365/


All Articles