简体   繁体   中英

How to access FIXTURES data from the Ember.Handlebars.helper?

This is how I have created my ember FIXTURE:

window.App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter;

App.Category = DS.Model.extend({
name: DS.attr(),
parent_id: DS.attr()
});

App.Category.FIXTURES = [
{
   id: 1,
   name: 'user1',
   email: 'user1@gmail.com',
   parent_id: 0
},
{
   id: 2,
   name: 'user2',
   email: 'user2@gmail.com',
   parent_id: 1
}
];

Here is a part of my ember view where 'parent-title' is a helper:

{{#each category in controller}}
<tr>
  <td>{{category.name}}</td>
  <td>{{parent-title category.parent_id}}</td>
  <td>Edit/Delete</td>
</tr>
{{/each}}

What I want is that during listing if the parent_id is 0 it should print 'master' else the name of parent category. In my exapmle parent of user2 is id=1 show it should print 'user1'. Below is the helper I have used:

Ember.Handlebars.helper('parent-title', function(parent_id){
  if (parent_id > 0) {
    var parent = category.findBy('id', parent_id);
    return parent.name;
  } else {
   return 'master';
  }
});

I know if I replace the line App.Category.FIXTURES = [ with var Category = [ I can get it done but I want ot do it with FIXTURES.

I can tell you that accessing data like that is a bad idea. If I were you, I would change parent_id to be a relationship, not an attribute (since that's really what it is). Then you can access the parent's name in templates with category.parent.name . Making it a relationship also gives you a few other luxuries.

But if you want to maintain backward compatibility, try using a computed property.

App.Category = DS.Model.extend({
    name: DS.attr(),
    parent_id: DS.attr(),

    parent: function() {
        return DS.PromiseObject.create({
            promise: this.get('store').find('category', this.get('parent_id'))
        });
    }.property('parent_id'),

    parent_name: function() {
        return this.get('parent.name');
    }.property('parent.name')
});

EDIT: If you want to change that into a relationship (which I think it should be), it's fairly simple. Instead of DS.attr , you use DS.belongsTo . For instances.

App.Categor = DS.Model.extend({
    name: DS.attr(),
    // I dropped the _id part because it's no longer an ID
    parent: DS.belongsTo('category', { inverse: null })
});

This tells Ember-Data to interpret the ID you give in the parent field as another category object. So category.get('parent') will return another category object, not a number. But in your case, to make it work, you'll have to convert all of the 0 IDs to null IDs. I wasn't sure if that was possible, which is why I recommended the computed property.

EDIT: To display master in case of a null parent, use the Handlebars if expression.

{{#if parent}}
    {{parent.name}}
{{else}}
    master
{{/if}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM