Store Ext assembly of data, Model

Version: 5.1.0

Said front: multiple Model and usually used in conjunction with a Store. Store a plurality of record collections (record is an example of the model).

For example: Store ultimate goal is to serve the components of Ext, like on checkbox (combobox) and mesh (grid). So, store is how to provide services to the components of it, see the code:

1. combobox:

    'use strict';


    Ext.onReady(function(){

        var states = Ext.create('Ext.data.Store', {
            fields: [ // it should be noted, Store automatically creates Model based on fields in the elements. 
                    // Fields may be empty, Ext field names based on the data, create your own Model 
            // 'abbr', 'name' 
            ],  
            Data: [ // inline data preparation may be inline, or can request, where only an example. 
                { "abbr": "AL", "name": "Alabama" },
                {"abbr":"AK", "name":"Alaska"},
                {"abbr":"AZ", "name":"Arizona"}
                //...
            ]
        });


        //上方的Store会根据fields创建一个类似这样的model
        Ext.define('MyComboModel', {
            extend: 'Ext.data.Model',
            fields: [  //fields可以为空,但实例化的时候要写明
                // {name: 'abbr',  type: 'string'},  
                // {name: 'name',  type: 'string'}
            ]
        });


        Ext.create('Ext.window.Window', {
            title: 'Hello',
            height: 200,
            width: 400,
            layout: 'fit',
            items:[
                {
                    xtype:'form',
                    items:[
                        {
                            xtype:'combobox',
                            fieldLabel: 'Choose State',
                            store: states,
                            queryMode: 'local',
                            displayField: 'name',
                            valueField: 'abbr',
                            id:'mycombo'
                        },{
                            xtype:'button',
                            text:'点击',
                            handler:function(){

                                console.log('nmsl');

                                // var curType = Ext.create('MyComboModel', {
                                //     abbr : 'CS',
                                //     name:'长沙'
                                // });

                                var curType = new MyComboModel({
                                    'abbr':'CS',
                                    'name':'长沙'
                                });

                                // Ext.getCmp('button').
                                Ext.getCmp('mycombo').setSelection(curType);

                            }
                        }
                    ]
                }
            ]
        }).show();

    });

 

Guess you like

Origin www.cnblogs.com/oy-lee/p/11410187.html