Proxy of extjs4.x Data Model

Source: Internet
Author: User
Tags sessionstorage

1. Basic overview
Proxy data proxy is the main way to perform data read/write operations. The data proxy classes provided by extjs mainly include Ext. data. proxy. client client proxy and ext. data. proxy. server server proxy, all of which are inherited from ext. data. proxy. proxy class. sub-classes of client proxy and server proxy are as follows:
Client proxy:
● Ext. Data. Proxy. Memory
● Ext. Data. Proxy. webstorage
● Ext. Data. Proxy. localstorage
● Ext. Data. Proxy. sessionstorage
Server Proxy:
● Ext. Data. Proxy. Ajax
● Ext. Data. Proxy. Rest
● Ext. Data. Proxy. jsonp
2. Ext. Data. Proxy. Proxy
Ext. data. proxy. proxy is the parent class of all proxy classes. It provides the basic data operation methods: crud, namely create (create), read (read), update (update), delete (delete ). Each crud method obtains the unique Ext. Data. Operation object as the parameter, which encapsulates the detailed content of the operation. In addition, each crud method can specify a callback function to process the data.
3. Ext. Data. Proxy. Memory
Ext. Data. proxymemory is a simple data proxy that uses memory variables to access data. Therefore, data is lost every time a page is refreshed.
1. (function (){
2. Ext. onready (function (){
3. Ext. regmodel ('user ',{
4. fields :[
5. {Name: 'name', type: 'string '},
6. {Name: 'age', type: 'int '}
7.]
8 .});
9.
10. var DATA = [
11. {'name': 'elvin. Cracker ', 'age': 24}
12.];
13.
14. var proxy = ext. Create ('ext. Data. Proxy. Memory ',{
15. Data: data,
16. Model: 'user'
17 .});
18.
19. Proxy. Read (New Ext. Data. Operation (), function (result ){
20. var array = result. resultset. records;
21. Ext. array. Each (array, function (record ){
22. Alert (record. Get ('name '));
23 .});
24 .});
25 .});
26 .})();
In fact, we can think that the data proxy is a bridge between the model and the data. The data proxy establishes the relationship between them and then performs related grud operations through the data proxy.
3. Ext. Data. Proxy. localstorage
Ext. data. proxy. localstorage is Ext. data. proxy. A subclass of webstorage, which is often used to save user information that does not need to be stored on the server. This information will not disappear after the browser is closed and can be saved for a long time.
1. (function (){
2. Ext. onready (function (){
3. Ext. regmodel ('user ',{
4. fields :[
5. {Name: 'name', type: 'string '},
6. {Name: 'age', type: 'int '}
7.],
8. Proxy :{
9. Type: 'localstore ',
10. ID: 'user-searchel'
11 .}
12 .});
13.
14. var store = new Ext. Data. Store ({
15. Model: User
16 .});
17.
18. Store. Add ({'name': 'elvin. Cracker ', 'age': 24 });
19.
20. // Save the data
21. Store. Sync ();
22.
23. // read data
24. Store. Load ();
25.
26. var message = []
27. Store. Each (function (record ){
28. Message. Push (record. Get ('name '));
29 .});
30. Alert (message. Join ('\ n '));
31 .});
32 .})();
4. Ext. Data. Proxy. sessionstorage
Ext. data. proxy. sessionstorage is Ext. data. proxy. the subclass of webstorage, which is different from ext. data. proxy. the difference between localstorage and localstorage is that when the browser is closed, data will be lost, and it is not suitable for long-term data storage. In addition, it is used in the same way as the localstorage proxy.
1. (function (){
2. Ext. onready (function (){
3. Ext. regmodel ('user ',{
4. fields :[
5. {Name: 'name', type: 'string '},
6. {Name: 'age', type: 'int '}
7.],
8. Proxy :{
9. Type: 'sessionstorage ',
10. ID: 'user-searchel'
11 .}
12 .});
13.
14. var store = new Ext. Data. Store ({
15. Model: User
16 .});
17.
18. Store. Add ({'name': 'elvin. Cracker ', 'age': 24 });
19.
20. // Save the data
21. Store. Sync ();
22.
23. // read data
24. Store. Load ();
25.
26. var message = []
27. Store. Each (function (record ){
28. Message. Push (record. Get ('name '));
29 .});
30. Alert (message. Join ('\ n '));
31 .});
32 .})();
5. Ext. Data. Proxy. Ajax
Ext. data. proxy. ajax is the most widely used data proxy method. It uses ajax to read and write data by requesting the specified URL. It cannot read cross-Origin data. If you need to read cross-Origin data, you can use Ext. data. proxy. the jsonp implementation is not described too much here.
1. (function (){
2. Ext. onready (function (){
3. Ext. regmodel ('user ',{
4. fields: [{Name: 'name', type: 'string'}],
5. Proxy :{
6. Type: 'sessionstorage ',
7. ID: 'user-searchel'
8 .}
9 .});
10.
11. var proxy = new Ext. Data. Proxy. Ajax ({
12. url: '/Proxy/ajax /',
13. Model: 'user ',
14. Reader: 'json ',
15. limitparam: 'indexlimit'
16 .});
17.
18. // Ext. Data. Operation () is a read operation by default.
19. Proxy. dorequest (New Ext. Data. Operation ({
20. Action: 'read ',
21. Limit: 10,
22. Start: 1,
23. sorters :[
24. New Ext. util. sorter ({
25. Property: 'name ',
26. Direction: 'asc'
27 .})
28.]
29.}), function (operation ){
30. var text = operation. response. responsetext;
31. Alert (ext. JSON. Decode (text) ['name']);
32 .});
33 .});
34 .})();
The data returned by the request server is parsed using Ext. JSON. Decode, And the request URL is/Proxy/ajax/. Here we use Django as the server language.
URLs. py
1. urlpatterns = patterns ('',
2. URL (R' ^ $ ', 'Views. home', name = 'home '),
3. URL (R' ^ proxy/ajax/', 'Views. Ajax '),
4 .)
Views. py
1. From Django. Shortcuts import render_to_response
2. From Django. Http import httpresponse
3. Import JSON
4.
5. Def home (request ):
6. Return render_to_response('ajax.html ')
7.
8. Def Ajax (request ):
9. Return httpresponse (JSON. dumps ({"name": "Elvin. Cracker"}), mimetype = "application/JSON ")
At the same time, let's take a look at the set of data parameters that firebug parses extjs passed to the background:
1. _ DC 1339857488033
2. indexlimit 10
3. Sort [{"property": "name", "direction": "ASC"}]
4. Start 1

 

Author: reminiscence kite

From: http://www.2cto.com/kf/201206/137162.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.