Guide applies to: modern
Introduction
In this lab, you’ll populate a data store from the the iTunes data feed.
Steps
1.Define the record
Create the folder and file app/desktop/src/model/Tune.js
with this code:
Ext.define('ModernTunes.model.Tune', {
extend: 'Ext.data.Model',
requires: ['Ext.data.proxy.JsonP'],
fields: [{
name: 'id',
mapping: 'id.attributes["im:id"]'
}, {
name: 'title',
mapping: '["im:name"].label'
}, {
name: 'image',
mapping: '["im:image"][2].label'
}, {
name: 'artist',
mapping: '["im:artist"].label'
}, {
name: 'itunesstore',
mapping: 'link[0].attributes.href'
}, {
name: 'preview',
mapping: 'link[1].attributes.href'
}, {
name: 'release_date',
mapping: '["im:releaseDate"].attributes.label'
}],
proxy: {
type: 'jsonp',
url: 'https://itunes.apple.com/us/rss/topmusicvideos/limit=50/json',
reader: {
type: 'json',
rootProperty: 'feed.entry'
}
}
});
2.Create the tunes store in the view model
Edit app/desktop/src/view/main/MainViewModel.js
, remove data object from the generated app Add a tunes
store that uses Tune
model with autoLoad: true
Ext.define('ModernTunes.view.main.MainViewModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.mainviewmodel',
requires: [
'ModernTunes.model.Tune'
],
stores: {
tunes: {
model: 'ModernTunes.model.Tune',
autoLoad: true
}
}
});
3.Test the store
Save your changes. Check the browser to view your updated changes. Using browser developer tools
, find itunes
in the network traffic in the ‘Network’ tab and you should see the call to https://itunes.apple.com/us/rss/topmusicvideos
.
(In versions before 6.5, the store is created +lazily+ — only after the view model is referenced. In that case, you wouldn’t see traffic yet.)
You can see how many records were fetched by running this on the console line:
Ext.first('mainview').getViewModel().getStore('tunes').getCount(); // 50
With the data now available to the app we can begin creating user interface compoonents that can take advantage it.