Reputation: 17000
I need to fill Store data on init with finction like:
Ext.define('Vcs.store.Years', {
extend: 'Ext.data.Store',
fields: ['year'],
data: this.getYears(),
getYears: function() {
var data = []
date = new Date
thisYear = date.getFullYear();
for(var i = thisYear - 100; i <= thisYear; i++) {
data.push({
year: i,
});
}
return data;
}
});
This code gives error: Object [object global] has no method 'generateYears'
Upvotes: 2
Views: 4988
Reputation: 12996
Neither solution suggested so far works in Sencha Architect: the closure gets all the whitespaces removed, thus new Date
becomes an undefined newDate
; and the function me.getYears()
gets quotes, thus becoming a string 'me.getYears()'
.
So for those who use Sencha Architect here's a version that should be used. I simply used the .add() method on store's "load" event.
Ext.define('MyApp.store.MyStore', {
extend: 'Ext.data.Store',
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
storeId: 'MyStore',
fields: [
{
name: 'year',
type: 'int'
}
],
listeners: {
load: {
fn: me.onStoreLoad,
scope: me
}
}
}, cfg)]);
},
onStoreLoad: function(store, records, successful, eOpts) {
store.add(function() {
var data = [];
var date = new Date();
var thisYear = date.getFullYear();
for (var i = thisYear - 100; i <= thisYear; i++) {
var obj = { year:i };
data.push(obj);
}
return data;
}());
}
});
Upvotes: 0
Reputation: 74126
You can do it this way:
Ext.define('Vcs.store.Years', {
extend: 'Ext.data.Store',
fields: ['year'],
data: (function() {
var data = [],
date = new Date,
thisYear = date.getFullYear();
for(var i = thisYear - 100; i <= thisYear; i++) {
data.push({
year: i
});
}
return data;
})()
});
Upvotes: 5
Reputation: 4421
I think the scoping of your this
is wrong. you'll have to populate the data in the constructor:
constructor: function() {
var me = this;
Ext.apply(me, {
data: me.getYears()
});
me.callParent();
}
Upvotes: 2