Topsy Extjs5 Ext.data.Model and Ext.data.Store (d) __js

Source: Internet
Author: User

First, Ext.data.Model

(1) Model represents some objects of application management. For example, we might define a model for some objects in the real world that we want to model in the system, like users, products, and automobiles. These models are registered in the Model manager and are used by the store, which is used by many data-bound components in ext.

The model is defined as a collection of fields, arbitrary methods, and model-related properties. For example:

Ext.define (' User ', {
    extend: ' Ext.data.Model ',
    fields: [
        {name: ' name ',  type: ' String '},
        {name: ' Age ',   type: ' int ', convert:null},
        {name: ' Phone ', type: ' String '},
        {name: ' Addree ', type: ' String '}
    ] ,

    changename:function () {
        var oldname = this.get (' name '),
            newName = oldname + "six finger harp";

        This.set (' name ', newName);
    }
);

Create an instance of the model user and invoke the logic of any model we define:

var user = ext.create (' user ', {
    name: ' Hex Demon ', age  :,
    Phone: ' 13800138000 '
});

User.changename ();


(2) model has built-in validation support, through the implementation of the validator function in Ext.data.validations to add validation to the models is very simple, such as:

Ext.define (' User ', {
    extend: ' Ext.data.Model ',
    fields: [
        {name: ' name ',     type: ' String '},
        {name: ' Age ',      type: ' int '},
        {name: ' phone ',    type: ' String '},
        {name: ' Gender ',   type: ' String '},
        { Name: ' username ', type: ' String '},
        {name: ' Alive ',    Type: ' Boolean ', Defaultvalue:true}
    ],

    Validations: [
        {type: ' Presence ',  field: ' Age '},
        {type: ' Length ',    field: ' Name ',     Min:2},
        {type: ' Inclusion ', field: ' Gender ',   list: [' Male ', ' Female ']},
        {type: ' Exclusion ', field: ' Username ', List: [' Admin ', ' Operator ']},
        {type: ' Format ',    field: ' Username ', Matcher:/([a-z]+) [0-9]{2,3}/}
    ]
});

By calling the Validate function, you can simply perform validation and return a Ext.data.Errors object:

var instance = ext.create (' User ', {
    name: ' Good source ',
    Gender: ' Male ',
    username: ' Hex magic '
});

var errors = Instance.validate ();

Second, Ext.data.Store

(1) The effect of the store is: warehouse, storage meaning. The store class encapsulates a client-side cache for storing Model objects. Stores loads data through proxy proxy and provides functions to sort, filter, and model instances contained within the query.
Creating a store is simple-we just need to pass in the Model and the proxy used to load/save the data as a configuration item:

Build a store to use the model
 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
 });

(2) Inline data

The Store can also load internally specified data. Internally, the Store converts the object we passed in as data to the model instance. Examples are as follows:

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

(3) Filtration and sorting

The Store can be sorted and filtered-two operations can be performed locally or remotely. The sorting sorters and filtering filters are all executed within the Mixedcollection instance, which makes it easy to manage and use. It is usually possible to specify sorters and filters in the configuration items in the store, and of course you can call the sort and filter methods:

var store = ext.create (' Ext.data.Store ', {
     model: ' User ',
     sorters: [{Property
         : ' Age ',
         direction: ' DESC '
     }, {property
         : ' FirstName ',
         direction: ' ASC '
     }],

     ///must contain the letter E or D
     filters: [{
         Property: ' FirstName ',
         value:/ed/
     }]
 };









Related Article

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.