Reputation: 488
I'm trying to learn emberJS and I figured a good starting example would be to map the relationships between the various elements in an RPG. Each element has a name and has multiple damage modifiers, depending on what element it is being used against. For example, Fire's modifier for Water is 0.5 but it's modifier for Grass is 2.0. I thought the simplest way to store this data was like so:
Pokechoice.Type.FIXTURES = [
{
id: 1,
name: 'normal',
damages: {
'normal': '1',
'fire': '1',
'water': '1',
'grass': '1',
'electric': '1',
'ice': '1',
'fighting': '1',
'poison': '1',
'ground': '1',
'flying': '1',
'psychic': '1',
'bug': '1',
'rock': '0.5',
'ghost': '0',
'dragon': '1',
'dark': '1',
'steel': '0.5',
'fairy': '1'
}
},
{
id: 2,
name: 'fire',
damages: {
'normal': '1',
'fire': '0.5',
'water': '0.5',
'grass': '2',
'electric': '1',
'ice': '2',
'fighting': '1',
'poison': '1',
'ground': '1',
'flying': '1',
'psychic': '1',
'bug': '2',
'rock': '0.5',
'ghost': '0',
'dragon': '0.5',
'dark': '1',
'steel': '2',
'fairy': '1'
}
}
];
But I'm having a hard time setting up a model for it. This is what I have so far:
Pokechoice.Type = DS.Model.extend({
name: DS.attr('string'),
damages: DS.hasMany('Pokechoice.Damage')
});
Pokechoice.Damage = DS.Model.extend({
name: DS.attr('string'),
modifier: DS.attr('number'),
type: DS.belongsTo('Pokechoice.Type')
});
Obviously it isn't working. Ideally I'd like to be able to have each Type data object contain all of its modifiers in a nested object, as shown above, but I can't find any tutorials or examples that do it this way. Any help is appreciated, I feel like I'm going about this the wrong way.
Upvotes: 0
Views: 90
Reputation: 1151
http://emberjs.jsbin.com/afojaZU/2/edit
One of the first big (time invested) answers I've made on SO :)
Upvotes: 1