0

Okay, so, without giving way too much detail, I'm trying to build an RPG character sheet as my first project with ember.js (though, I've already done the TodoMVC tutorial, as well as Tom Dale's video guide, and the Peepcode video. It is, however, likely that I've already lost/forgotten bits and pieces, so forgive me if this question is covered there).

Basically, just tracking character stats and whatnot. Most of the stats are pretty straightforward, as they're just strings/numbers. What's tripping me up a bit is when I get to things like character inventory, skills, etc.

For example, in trying to set up my data model, I've got this for the simple attributes:

App.Character = DS.Model.extend({
  species: DS.attr('string'),
  career: DS.attr('string'),
  gender: DS.attr('string'),
  age: DS.attr('number'), 
  height: DS.attr('number'),
  hair: DS.attr('string'),
  notableFeatures: DS.attr('string'),
  (etc., etc.)
});

Now, for other attributes, like, say, an astrogation skill, I've got both the skill name and the rank the character has in that skill.

One option would be to just get kind of lazy and set up each skill as follows:

  skillAstrogation: DS.attr('number')

I don't feel like that's quite in the "spirit" of things, though, and it would make it harder for me to add/remove skills later on if necessary. Also, I'd like, at some point, for the user to be able to add their own skills.

So, after asking on reddit, and doing some more research, it looks like I should be using a many-to-many relationship. That is, set up a separate model for "skills", and give each a name, rank, etc. (there's another field or two I'll want, as well).

Something like this:

App.Character = DS.Model.extend({
  ...
  skills: DS.hasMany("App.Skill")
});

App.Skill = DS.Model.extend({
  title: DS.attr('string'),
  rank: DS.attr('number'),
  characters: DS.hasMany("App.Character")
});

What I'm not entirely clear on, though, is how I then set the rank for each character per-skill, if that makes sense.

Maybe "rank" shouldn't be in App.Skill? Should it be in the Character model, instead?

I hope I'm being clear, here. :)

Thanks for any input!

4

1 回答 1

0

As I understand correctly what you want is not actually many to many but many to one. Each skill rank only corresponds with one Character right? Now you might feel that this is adding a lot of duplicate content since the title would be copied each time, in this case you would actually like 3 models.

Something like this:

App.Character = DS.Model.extend({
  ...
  skills: DS.hasMany("App.Linkage")
});

App.Linkage = DS.Model.extend({
    rank: DS.attr('number'),
    character: DS.belongsTo("App.Character")
    skill: DS.belongsTo("App.Skill")
});

App.Skill = DS.Model.extend({
    title: DS.attr('string'),
    ...
});

To now retrieve the data you could do the following:

route:

model: function () {
    return App.Character.find();
}

template:

 <ul>
        {{#each character in controller}}
            <li>{{character.name}} -
                {{#each item in character.skills }}
                    {{item.skill.skill}}: {{item.rank}},
                {{/each}}
            </li>
         {{/each}}
    </ul>
于 2013-07-27T09:36:01.410 回答