Created
January 9, 2013 09:04
-
-
Save drillbits/4491728 to your computer and use it in GitHub Desktop.
Sencha Touch 2 でデータを取得してくるときの proxy とかの使い方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 適当なモデル | |
Ext.define('app.model.Entry', { | |
extend: 'Ext.data.Model', | |
config: { | |
fields: [ | |
{name: 'title', type: 'string'}, | |
{name: 'body', type: 'string'} | |
] | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// モデルに対応するストア | |
Ext.define('app.store.Entry', { | |
extend: 'Ext.data.Store', | |
config: { | |
model: 'app.model.Entry', | |
// データを Ajax で取得する定義 | |
proxy: { | |
type: 'ajax', | |
url: 'http://example.com/path/to/api', | |
// クエリパラメータが必要だが、ここでは以下のような呼び出しはできない | |
// extraParams: { | |
// spam: list.getSpam() | |
// }, | |
reader: { | |
type: 'json', | |
rootProperty: 'entries' | |
} | |
}, | |
// 初期化時にはまだ取得しない | |
autoLoad: false | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ext.define('app.controller.List', { | |
extend: 'Ext.app.Controller', | |
config: { | |
refs: { | |
list: 'view_list' | |
}, | |
control: { | |
list: { | |
show: 'loadListData', | |
} | |
} | |
}, | |
loadListData: function(list) { | |
var store = list.getStore(); | |
// store の proxy を上書きできる | |
// 設定しなかったキー(reader とか)は元のやつが使われる | |
store.setProxy({ | |
// url を書き換えるとローカルのダミーデータを使うように書き換えられる | |
url: 'data/dummy.json', | |
// クエリパラメータも追加で設定できるので、ここでやる | |
extraParams: { | |
spam: list.getSpam() | |
} | |
}); | |
// Ajax でデータを取得する | |
// データを加工してリストに出力などはよしなにしてくれる | |
store.load(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 適当なリスト | |
Ext.define('app.view.List', { | |
extend: 'Ext.dataview.List', | |
config: { | |
store: 'Entry', | |
itemTpl: '{title}' | |
}, | |
getSpam: function() { | |
return 'spamspamspamspamspam'; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment