ExtJS筆記 Field

來源:互聯網
上載者:User

標籤:

Fields are used to define what a Model is. They aren‘t instantiated directly - instead, when we create a class that extendsExt.data.Model, it will automatically create a Field instance for each field configured in a Model. For example, we might set up a model like this:

欄位用於定義model是什麼。它們不會被直接執行個體化--做為替代,我們建立model類,它們會根據field配置項自動建立每個欄位的執行個體。例如,我們建立下面的model:

Ext.define(‘User‘, {    extend: ‘Ext.data.Model‘,    fields: [        ‘name‘, ‘email‘,        {name: ‘age‘, type: ‘int‘},        {name: ‘gender‘, type: ‘string‘, defaultValue: ‘Unknown‘}    ]});

 

Four fields will have been created for the User Model - name, email, age and gender. Note that we specified a couple of different formats here; if we only pass in the string name of the field (as with name and email), the field is set up with the ‘auto‘ type. It‘s as if we‘d done this instead:

為會user模型建立四個欄位--name、email、age、gender。注意,這裡我們指定了一對不同的格式。如果我們僅僅傳入欄位的名稱(對於name和email),欄位會被建立為auto類型。就像我們這樣做:

Ext.define(‘User‘, {    extend: ‘Ext.data.Model‘,    fields: [        {name: ‘name‘, type: ‘auto‘},        {name: ‘email‘, type: ‘auto‘},        {name: ‘age‘, type: ‘int‘},        {name: ‘gender‘, type: ‘string‘, defaultValue: ‘Unknown‘}    ]});

 

Types and conversion   類型和轉換

The type is important - it‘s used to automatically convert data passed to the field into the correct format. In our example above, the name and email fields used the ‘auto‘ type and will just accept anything that is passed into them. The ‘age‘ field had an ‘int‘ type however, so if we passed 25.4 this would be rounded to 25.

類型很重要--它對傳給field的資料使用自動轉換,轉換為正確的格式。在上面的例子中,name和email欄位使用auto類型,並將介紹任何傳給它們的值。然而,age欄位是int類型,因此,如果我們穿25.4給它,會被自動四捨五入為25.

Sometimes a simple type isn‘t enough, or we want to perform some processing when we load a Field‘s data. We can do this using a convert function. Here, we‘re going to create a new field based on another:

有時,一個簡單的類型是不夠的,或者我們希望在載入資料的時候執行一些處理。我們可以使用convert函數做到這點。這裡,我們將建立一個基於其它欄位的新欄位:

Ext.define(‘User‘, {    extend: ‘Ext.data.Model‘,    fields: [        {            name: ‘firstName‘,            convert: function(value, record) {                var fullName  = record.get(‘name‘),                    splits    = fullName.split(" "),                    firstName = splits[0];                return firstName;            }        },        ‘name‘, ‘email‘,        {name: ‘age‘, type: ‘int‘},        {name: ‘gender‘, type: ‘string‘, defaultValue: ‘Unknown‘}    ]});

 

Now when we create a new User, the firstName is populated automatically based on the name:

現在,當我們建立一個新user,firstName會基於name自動產生:

var ed = Ext.create(‘User‘, {name: ‘Ed Spencer‘});console.log(ed.get(‘firstName‘)); //logs ‘Ed‘, based on our convert function

 

Fields which are configured with a custom convert function are read after all other fields when constructing and reading records, so that if convert functions rely on other, non-converted fields (as in this example), they can be sure of those fields being present.

具有自訂convert函數配置的自動,會在其它欄位之後被構造和讀取。

In fact, if we log out all of the data inside ed, we‘ll see this:

事實上,如果我們log輸出所有ed中的資料,我們會看到:

console.log(ed.data);//outputs this:{    age: 0,    email: "",    firstName: "Ed",    gender: "Unknown",    name: "Ed Spencer"}

 

The age field has been given a default of zero because we made it an int type. As an auto field, email has defaulted to an empty string. When we registered the User model we set gender‘s defaultValue to ‘Unknown‘ so we see that now. Let‘s correct that and satisfy ourselves that the types work as we expect:

age自動被賦予了一個為0的預設值,因為它們是int類型。作為一個auto欄位,email的預設值是一個Null 字元串。當我們註冊usermodel,gender的預設值設為’Unknown‘。讓我們修正這個值,以使其如我們期望的來工作:

ed.set(‘gender‘, ‘Male‘);ed.get(‘gender‘); //returns ‘Male‘ed.set(‘age‘, 25.4);ed.get(‘age‘); //returns 25 - we wanted an int, not a float, so no decimal places allowed

 

ExtJS筆記 Field

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.