ExtJS筆記 Store

來源:互聯網
上載者:User

標籤:

The Store class encapsulates a client side cache of Model objects. Stores load data via a Proxy, and also provide functions for sorting, filtering and querying the model instances contained within it.

 Store類封裝了Model對象在用戶端的緩衝。store通過proxy載入資料,並且提供排序、過濾、查詢model執行個體的函數。

Creating a Store is easy - we just tell it the Model and the Proxy to use for loading and saving its data:

建立store很簡單--我們只需告訴它model和proxy:

// Set up a model to use in our Store Ext.define(‘User‘, {     extend: ‘Ext.data.Model‘,     fields: [         {name: ‘firstName‘, type: ‘string‘},         {name: ‘lastName‘,  type: ‘string‘},         {name: ‘age‘,       type: ‘int‘},         {name: ‘eyeColor‘,  type: ‘string‘}     ] }); var myStore = Ext.create(‘Ext.data.Store‘, {     model: ‘User‘,     proxy: {         type: ‘ajax‘,         url: ‘/users.json‘,         reader: {             type: ‘json‘,             root: ‘users‘         }     },     autoLoad: true });

 

In the example above we configured an AJAX proxy to load data from the url ‘/users.json‘. We told our Proxy to use aJsonReader to parse the response from the server into Model object - see the docs on JsonReader for details.

在上面的例子中,我們配置了一個ajax代理來從‘/users.json‘載入資料。我們告訴代理使用JsonReader解析服務端的響應到model對象。詳情參見see the docs on JsonReader

Inline data  內聯資料

Stores can also load data inline. Internally, Store converts each of the objects we pass in as data into Model instances:

store可通過內聯方式載入資料。在內部,store轉換每個我們傳入的的對象為model執行個體:

 Ext.create(‘Ext.data.Store‘, {     model: ‘User‘,     data : [         {firstName: ‘Ed‘,    lastName: ‘Spencer‘},         {firstName: ‘Tommy‘, lastName: ‘Maintz‘},         {firstName: ‘Aaron‘, lastName: ‘Conran‘},         {firstName: ‘Jamie‘, lastName: ‘Avins‘}     ] });

 

Loading inline data using the method above is great if the data is in the correct format already (e.g. it doesn‘t need to be processed by a reader). If your inline data requires processing to decode the data structure, use a MemoryProxy instead (see the MemoryProxy docs for an example).

如果資料格式是正確的(不需要我們提供reader來處理),那麼使用內聯資料是很好的。如果內聯資料需要解析資料格式,那麼要使用MemoryProxy 。

Additional data can also be loaded locally using add.

額外的資料可以通過add方法來載入。

Dynamic Loading   動態載入

Stores can be dynamically updated by calling the load method:

store可以通過調用load方法來動態更新:

store.load({    params: {        group: 3,        type: ‘user‘    },    callback: function(records, operation, success) {        // do something after the load finishes    },    scope: this});

 

Here a bunch of arbitrary parameters is passed along with the load request and a callback function is set up to do something after the loading is over.

這個例子中,一組任意的參數通過load方法傳入,回呼函數在使得load結束時候可以做些事情。

Loading Nested Data    載入嵌套資料

Applications often need to load sets of associated data - for example a CRM system might load a User and her Orders. Instead of issuing an AJAX request for the User and a series of additional AJAX requests for each Order, we can load a nested dataset and allow the Reader to automatically populate the associated models. Below is a brief example, see the Ext.data.reader.Reader intro docs for a full explanation:

app經常需要載入一組相關的資料--例如,crm系統會載入users和它的orders。一種方式是通過一個ajax請求得到user,通過一系列額外的請求得到每個訂單,我們可以載入一個嵌套的資料集,並讓reader自動組裝相關的模型,詳見Ext.data.reader.Reader:

 var store = Ext.create(‘Ext.data.Store‘, {     autoLoad: true,     model: "User",     proxy: {         type: ‘ajax‘,         url: ‘users.json‘,         reader: {             type: ‘json‘,             root: ‘users‘         }     } });

 

Which would consume a response like this:

我們將消費這樣一個響應:

 {     "users": [{         "id": 1,         "name": "Ed",         "orders": [{             "id": 10,             "total": 10.76,             "status": "invoiced"        },{             "id": 11,             "total": 13.45,             "status": "shipped"        }]     }] }

 

See the Ext.data.reader.Reader intro docs for a full explanation.

Filtering and Sorting     過濾和排序

Stores can be sorted and filtered - in both cases either remotely or locally. The sorters and filters are held inside MixedCollection instances to make them easy to manage. Usually it is sufficient to either just specify sorters and filters in the Store configuration or call sort or filter:

store可以被排序和過濾--並都支援遠程和本地模式。MixedCollection內部管理著sorters和 filters 。通過stroe配置項指定sorters 和filters ,或直接調用sort或filter,兩種方式皆可。

 var store = Ext.create(‘Ext.data.Store‘, {     model: ‘User‘,     sorters: [{         property: ‘age‘,         direction: ‘DESC‘     }, {         property: ‘firstName‘,         direction: ‘ASC‘     }],     filters: [{         property: ‘firstName‘,         value: /Ed/     }] });

 

The new Store will keep the configured sorters and filters in the MixedCollection instances mentioned above. By default, sorting and filtering are both performed locally by the Store - see remoteSort and remoteFilter to allow the server to perform these operations instead.

這個新store會在MixedCollection 執行個體中儲存sorters 和filters 的配置。預設的,排序和過濾都是在本地操作,--參見remoteSort和remoteFilter 來允許伺服器執行操作。

Filtering and sorting after the Store has been instantiated is also easy. Calling filter adds another filter to the Store and automatically filters the dataset (calling filter with no arguments simply re-applies all existing filters). Note that by default sortOnFilter is set to true, which means that your sorters are automatically reapplied if using local sorting.

store執行個體化後,修改過濾和排序也很簡單。調用filter添加其它的過濾並自動過濾資料集(不帶參數直接調用filter會重新應用現有的過濾)。注意,sortOnFilter預設的是true,這意味著你的sorter會自動重新應用。

store.filter(‘eyeColor‘, ‘Brown‘);

 

Change the sorting at any time by calling sort:

任何時候,通過調用sort方法改變排序:

store.sort(‘height‘, ‘ASC‘);

 

Note that all existing sorters will be removed in favor of the new sorter data (if sort is called with no arguments, the existing sorters are just reapplied instead of being removed). To keep existing sorters and add new ones, just add them to the MixedCollection:

注意所有現存的排序都會被取代(如果sort以無參數形式調用,現存的sorter會重新應用而不是移除)。如果要保留現有的排序並加入新的,則將它們加到MixedCollection:

store.sorters.add(new Ext.util.Sorter({    property : ‘shoeSize‘,    direction: ‘ASC‘}));store.sort();

 

Registering with StoreManager   在StoreManager中註冊

Any Store that is instantiated with a storeId will automatically be registered with the StoreManager. This makes it easy to reuse the same store in multiple views:

任何以一個storeId執行個體化的store都會自動註冊到StoreManager。這使得很容易在多個視圖間重用同一個store:

//this store can be used several timesExt.create(‘Ext.data.Store‘, {    model: ‘User‘,    storeId: ‘usersStore‘});new Ext.List({    store: ‘usersStore‘,    //other config goes here});new Ext.view.View({    store: ‘usersStore‘,    //other config goes here});

 

Further Reading   延伸閱讀

Stores are backed up by an ecosystem of classes that enables their operation. To gain a full understanding of these pieces and how they fit together, see:

store基於一個類系統構建。要完整的理解它們,並清楚它們如何一起工作,請看:

  • Proxy - overview of what Proxies are and how they are used
  • Model - the core class in the data package
  • Reader - used by any subclass of ServerProxy to read a response

ExtJS筆記 Store

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.