모바일/Hybrid
센차터치에서 바로 원격 데이터 뽑아오기
늘근이
2015. 8. 10. 23:26
센차터치는 쉬운것 같으면서도 뭔가를 구현하려 하면 항상 2시간동안 삽질을 해야 한다.
참조문서도 많이 없고 튜토리얼도 구려서 그렇다.
보통 아래와 같이 store를 선언하면 원격지에서 데이터를 불러오게끔 세팅이 되어있다.
현재는 Express기반으로 한 node.js 에 mongoDB를 붙인 형태이기 때문에 조금 복잡하다.
다만 REST 하게 구현했기 때문에 어느정도 직관적이다.
Ext.define('attack.store.ShopStore', { extend: 'Ext.data.Store', requires: [ 'attack.model.Shop' ], config: { autoLoad: true, model: 'attack.model.Shop', storeId: 'ShopStore', proxy: { type: 'jsonp', url: 'http://localhost:3000/list', reader: { type: 'json', idProperty: '_id', rootProperty: 'records', useSimpleAccessors: true } } } });
Ext.define('attack.model.Shop', { extend : 'Ext.data.Model', config : { fields: ['_id', 'name', 'category','desc', 'time', 'lng', 'lat' ], // proxy : { // type : 'rest', // url : 'data/shops', // reader : { // type: 'json', // root: 'shops', // } // } }, });
그리고 아래와 같이 세팅을 해보자.
var storeTemp = Ext.getStore('ShopStore'); storeTemp.load({ scope:this, callback: function(records) { console.log(records) var data = records[0].getData().name; console.log(data) } });
나중에 콘솔로 로그를 찍어보면 아래와 같다.
getData()메서드는 아무데서나 작동하지 않고, records[0]처럼 직접 지정해준 데이터에서 데이터를 뽑아오는 역할을 한다. 따라서, 특정 데이터를 뽑아오고 싶으면 .name 과같은 멤버변수를 주어 뽑아올수 있다.