[LsitView & JSON 사용하기]

 

1. JASON 이란?

. 모바일 환경등에서 Data를 보다 편리하고, 가볍게 처리하기 위한 양식 ( <-> XML )

. Format : {}, [], ”,” 등으로 구성

. 구성예

{

        result:[

                   {

                             'name':'종현1',

                             'email':'gmail1@gmail.com',

                             'phone':'11111',                       

                   },

                   {

                             'name':'종현2',

                             'email':'gmail2@gmail.com',

                             'phone':'22222',                       

                   },

                   {

                             'name':'종현3',

                             'email':'gmail3@gmail.com',

                             'phone':'33333',                       

                   }                                         

        ]

}

 

2. JASON 파일 만들기

 .  [프로젝트 홈]에서 contact.jason 파일을 만들고, 위 예제 파일 내용을 저장

 

3. Data Model 파일 만들기

. [프로젝트 홈]> 터미널 실행

./sdk/command/sencha generate model -n ContactModel -f

name,email,phone

. [프로젝트홈]/app/model/ContactModel.js 파일 확인

 

Ext.define('Sencha.model.ContactModel', {

    extend: 'Ext.data.Model',

   

    config: {

        fields: [

            {name: 'name', type: 'auto'},

            {name: 'email', type: 'auto'},

            {name: 'phone', type: 'auto'}

        ]

    }

});

 

3. ListView 파일 작성

 . [프로젝트홈]/app/view/ ListView.js 추가

 . 아래 내용 추가

 . contact.jason / ContactModel 데이터를 이용하여, ContactStore 파일 구현

 . ContactStore를 이용하여, ContactList 구현

 . ContactList 파일을 이용하여, config.items에 추가

 . 파일 구현 내용

  

Ext.define(         'Sencha.view.ListView',{

           extend:'Ext.Panel',

           xtype : 'listview',

           config:{

                     layout:'vbox',

           },

          

           constructor: function(config) {

                      config = config ||          {};

          

                      Ext.define('ContactStore', {

                                extend: 'Ext.data.Store',

                                config: {

                                           model: 'Sencha.model.ContactModel',

                                           autoLoad: true,

                                           proxy: {

                                                     type: 'ajax',

                                                     url: 'contact.json',

                                                     extraParams: '',

                                                     reader: {

                                                               type: 'json',

                                                               rootProperty: 'result',

                                                     },

                                           },

                               },

                      });

          

                      Ext.define('ContactList', {

                               extend:'Ext.List',

                               xtype:'contactlist',

                              

                               config:{

                                          store:Ext.create('ContactStore'),

                                          disableSelection:false,

                                          emptyText:'<p> No Data <p>',

                                          itemTpl:Ext.create('Ext.XTemplate', '<h2>{name} : {email}: {phone} </h2>' ),

                               }

                      });

          

                      config.items = [

                                {

                                          xtype: 'toolbar',

                                          title: 'List',

                                },

                                {

                                         

                                           xclass: 'ContactList',

                                           flex: 1,

                                }

                      ];

                      

                      this.callParent([config]);

           }

});

 

4. controller  구현하기

1) ListViewController 만들기

  . [프로젝트홈] 터미널 실행

  . command 실행

./sdk/command/sencha generate controller -n ListViewController

2) control 이벤트 및 호출함수 구현

Ext.define('Sencha.controller.ListViewController', {

    extend: 'Ext.app.Controller',

   

    config: {

        refs: {

           

        },                

        control: {

                             contactlist: {

                                        itemtap: 'itemSelected',

                             },

        }

    },

   

    itemSelected: function(list, index, node, record) {

                  console.log(list);

                  console.log(index);

                  console.log(node);

                  console.log(record);

    },

});

 

 

Posted by 꿈을펼쳐라
,