Ext.js 數(shù)據(jù)

2021-12-06 14:03 更新

數(shù)據(jù)包用于加載和保存應用程序中的所有數(shù)據(jù)。

數(shù)據(jù)包有許多類,但最重要的類是:

  1. 模態(tài)
  2. 商店
  3. 代理

模型

modal的基類是Ext.data.Model.It表示應用程序中的一個實體。 它將存儲數(shù)據(jù)綁定到視圖。 它有后端數(shù)據(jù)對象到視圖dataIndex的映射。 在store的幫助下獲取數(shù)據(jù)。

創(chuàng)建模型

為了創(chuàng)建一個模型,我們需要擴展Ext.data.Model類,我們需要定義字段的名稱和映射。

   Ext.define('StudentDataModel', {
      extend: 'Ext.data.Model',
      fields: [
      {name: 'name', mapping : 'name'},
      {name: 'age', mapping : 'age'},
      {name: 'marks', mapping : 'marks'}
      ]
   });

這里的名稱應該與我們在視圖中聲明的dataIndex相同,并且映射應該匹配使用store從數(shù)據(jù)庫獲取的靜態(tài)或動態(tài)數(shù)據(jù)。

商店

store的基類是Ext.data.Store。 它包含本地緩存的數(shù)據(jù),該數(shù)據(jù)將在模型對象的幫助下在視圖上呈現(xiàn)。 存儲使用代理獲取數(shù)據(jù),代理具有為服務定義的路徑以獲取后端數(shù)據(jù)。

存儲數(shù)據(jù)可以從靜態(tài)或動態(tài)兩種方式獲取。

靜態(tài)存儲

對于靜態(tài)存儲,我們將存儲在存儲中的所有數(shù)據(jù)如下:

   Ext.create('Ext.data.Store', {
      model: 'StudentDataModel',
      data: [
         { name : "Asha", age : "16", marks : "90" },
         { name : "Vinit", age : "18", marks : "95" },
         { name : "Anand", age : "20", marks : "68" },
         { name : "Niharika", age : "21", marks : "86" },
         { name : "Manali", age : "22", marks : "57" }
      ];
   });

動態(tài)存儲

可以使用代理獲取動態(tài)數(shù)據(jù)。 我們可以讓代理可以從Ajax,Rest和Json獲取數(shù)據(jù)。

代理

代理的基類是Ext.data.proxy.Proxy。 代理由模型和商店用于處理模型數(shù)據(jù)的加載和保存。

有兩種類型的代理:

  1. 客戶端代理
  2. 服務器代理

客戶端代理

客戶端代理包括使用HTML5本地存儲的內(nèi)存和本地存儲。

服務器代理

服務器代理使用Ajax,Json數(shù)據(jù)和Rest服務處理來自遠程服務器的數(shù)據(jù)。

定義服務器中的代理:

Ext.create('Ext.data.Store', {
   model: 'StudentDataModel',
   proxy : {
      type : 'rest',
      actionMethods : {
         read : 'POST'  // Get or Post type based on requirement
      },
      url : 'restUrlPathOrJsonFilePath', // here we have to include the rest URL path which fetches data from database or Json file path where the data is stored
      reader: {
         type : 'json',  // the type of data which is fetched is of JSON type
         root : 'data'
      },
   }
});
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號