问题
I'm totally new to sencha touch and extjs. Here is the scenario:
Suppose you have this store that holds the items on a shopping cart:
Ext.define('MyApp.store.CardItems', {
extend: 'Ext.data.Store',
requires: ['MyApp.model.CardItem','Ext.data.proxy.SessionStorage'],
config :{
model: 'MyApp.model.QuoteItem',
autoLoad: true,
autoSync: true,
identifer: 'uuid',
proxy:{
type:'sessionstorage',
id:'card'
}
}
});
I know that in sencha a store
is used to bind a list of model items to a grid for example. I just wonder if it's a good idea to also add custom methods to this class.
For instance. Suppose we need a method to get the overall count of all items in the store. From an OOP point of view it makes sense to me to introduce such method directly on the store:
getOverallQuantity:function () {
var sum = 0;
this.each(function (item) {
sum += parseInt(item.get('amount'));
}, this);
return sum;
}
I just wonder if it's a good idea to have those methods directly on the store or if that is to avoid for some reason?
回答1:
Go ahead Christoph with extending your classes, it is your business, it is OK :)
I'm creating large hierarchy of stores/models/proxies and all is working. Sencha Foundation Classes (SFC!) it cool!
Cheers, Oleg
回答2:
I don't see anything wrong with a hierarchical approach but the inheritance would be kind of "weird" imo. The business logic would be tied to a model .. certain fields and what not but the methods would be on the store, which would accept only certain kinds of models. With some minor tweaks to store you can have your business logic methods exist on the model. It is all up to personal preference, the amount of models needed, how the fields are tied together, etc.
...
I know this is sample code but putting objects like config
on the protype is bad practice and can lead to sharing of instance errors. Your example would probably expose proxy sharing problems.
var newCards = new MyApp.store.CardItems(),
oldCards = new MyApp.store.CardItems();
newCards.load({params: {status: 'new'}});
oldCards.load({params: {status: 'old'}});
Whichever store that loads first will have the data overwritten because the proxies are shared. This scenario can be avoided by doing something like :
Ext.define('MyApp.store.CardItems', {
extend: 'Ext.data.Store',
requires: ['MyApp.model.CardItem', 'Ext.data.proxy.SessionStorage'],
constructor: function() {
Ext.apply(this, {
model: 'MyApp.model.QuoteItem',
autoLoad: true,
autoSync: true,
identifer: 'uuid',
proxy: {
type: 'sessionstorage',
id: 'card'
}
});
this.callParent(arguments)
}
});
回答3:
I don't see anything wrong with your proposal. It is just worth remembering that instead of using extend you can also override ExtJs' base classes, like so:
Ext.onReady(function(){
Ext.override(Ext.panel.Panel, {
// Add functionality to a panel to determine
// if it's the active tab within a tab panel or not
isActiveTab: function()
{
var owningTabPanel = this.up('tabpanel');
if ( owningTabPanel )
{
return owningTabPanel.getActiveTab() == this;
} else {
return false;
}
}
});
});
来源:https://stackoverflow.com/questions/12013298/should-i-add-custom-methods-to-ext-data-store-derived-classes