我知道这可能听起来很傻,希望这不会让人觉得太蠢。我想要使用多个商店来填充一个数据视图或其他可以应用 itemTpl 的项目。 我有一个基本的想法要实现:
如上面的图片所示,第一个用户可以看到所有物品,没有任何物品是变灰的(因为他们有足够的余额),但是第二个用户没有足够的钱来购买装备。有没有办法从两个不同的商店(ItemStore 和 UserStore)中获取信息以列出所有物品,但是将用户买不起的物品变灰?
下面是我的代码:
- 我有一组用户和物品,用户可以购买物品。
- 每个用户都有一个关联余额,它决定了他们可以购买哪些物品。
如上面的图片所示,第一个用户可以看到所有物品,没有任何物品是变灰的(因为他们有足够的余额),但是第二个用户没有足够的钱来购买装备。有没有办法从两个不同的商店(ItemStore 和 UserStore)中获取信息以列出所有物品,但是将用户买不起的物品变灰?
下面是我的代码:
(DataView) 物品选择屏幕:
Ext.define("touchstore.view.purchase.ItemSelectionScreen", {
extend: 'Ext.dataview.DataView',
xtype: 'item-selection',
config: {
store: 'ItemStore',
itemTpl: new Ext.XTemplate(
'<tpl for=".">',
'<div class="item {canAfford}">', //canAfford should be that the users balance is greater than the price of the item
'<h1>{itemName}</h1>',
'<span>{description}</span><br />',
'<div class="cost"><strong>Cost: {cost}</strong></div>',
'</div>',
'</tpl>')
}
});
ItemModel:
Ext.define('touchstore.model.ItemModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'itemName', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'cost', type: 'int' }
]
}
});
ItemStore:
Ext.define("touchstore.store.ItemStore", {
extend: 'Ext.data.Store',
config: {
model: 'touchstore.model.ItemModel',
data: [
{ itemName: 'Sword', description: 'Battle your enemies fiercely with this beautiful, yet deadly sword.', cost: 500 },
{ itemName: 'Armour', description: 'Defend yourself from attacks with this beautifully crafted piece of armour.', cost: 1400 },
{ itemName: 'Leather Boots', description: 'Run faster with these lightweight yet stylish boots.', cost: 400 }
]
}
});
UserModel:
Ext.define("touchstore.model.UserModel", {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'username', type: 'string' },
{ name: 'balance', type: 'int' }
]
}
});
UserStore:
Ext.define("touchstore.store.UserStore", {
extend: 'Ext.data.Store',
config: {
model: 'touchstore.model.UserModel',
data: [
{ username: 'steve.smith', balance: 2000 },
{ username: 'robert.junior', balance: 500 }
]
}
});