如何将对象传递给Ember.Handlebars.helper

时间:2014-12-10 23:05:03

标签: ember.js handlebars.js

我正在试图找出如何通过把手传递和嵌入对象到辅助函数。有关如何做到这一点的任何想法?

数据

groups = [
  {
    id:'as93-2dsa',
    name:'read'
  },
  {
    id:'dk20-d23k',
    name: 'write'
  },
  {
    id: 'mn20-8al3',
    name: 'execute'
  }
];
user = {
  id:'lk30-a8bk',
  name: 'Seth'
  groupIDs:[
    'as93-2dsa',
    'lk30-a8bk'
  ]
}

HTML

{{inGroup groups user}}

控制器

Ember.Handlebars.helper('inGroup', function(groups, user) {

/** 
 * At this point I would expect groups to be and object and user to be an object
 * but both return classes
 */

  console.log(groups); 
  //Class {toString: function, __ember1418251045900: "ember646", __nextSuper: undefined, __ember_meta__: Object, constructor: function…}
  console.log(groups.toString()); 
  // "<DS.PromiseArray:ember646>"

  console.log(user); 
  //Class {id: "5CF3A051-12B4-436C-B43A-5696F2792B55", store: Class, container: Container, _changesToSync: Object, _deferredTriggers: Array[0]…}
  console.log(user.toString()); 
  // "<nfors-app@model:user::ember526:5CF3A051-12B4-436C-B43A-5696F2792B55>"

/**
 * I would like to access groups[i].id and user.groupIds in a way such as the following
 */

  for (var i = 0; i < groups.length; i++) {
    if (user.groupIds.indexof(groups[i].id) !== -1) {
      return true;
    }
  }
  return false;
});

用户模型

import DS from 'ember-data';
import EmberValidations from 'ember-validations';

export default DS.Model.extend(EmberValidations.Mixin, {
    name:       DS.attr('string'),
    groupIds:    DS.hasMany('groupIds', { async: true }),

    validations: {
        name: {
            presence: true,
            length: { minimum: 5 }
        }
    }
});

0 个答案:

没有答案