简体   繁体   English

灰烬中的助手正在收到“未定义”

[英]helper in ember is receiving “undefined”

I have a component in ember that contains two separate columns. 我在ember中有一个组件,其中包含两个单独的列。 I am using a helper to take the array and split that array into two, displaying evens on the first column and odds on the left. 我正在使用一个帮助器来获取数组并将该数组拆分为两个,在第一列显示偶数,在左侧显示几率。 In the console it shows that the helper is receiving an undefined object. 在控制台中,它表明助手正在接收未定义的对象。

index.hbs: index.hbs:

<div class="row" style="padding:0% 3%">
    {{component numbers=model}}
</div>

index.js: index.js:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
    model() {
        return this.store.findAll('number');
    }
});

component.hbs (component with two columns): component.hbs(具有两列的组件):

<div class="row">
<div class="small-6 columns">
{{#each (twoColumns numbers 1) as |number|}}
    <h3><b>{{number.value}}</b></h3>
    <h6>{{number.title}}</h6>
{{/each}}
</div>
{{#each (twoColumns numbers 2) as |number|}}
    <h3><b>{{number.value}}</b></h3>
    <h6>{{number.title}}</h6>
{{/each}}
</div>
</div>

twoColumns.js (the helper): twoColumns.js(帮助程序):

import Ember from 'ember';

export function twoColumns(params) {
  let arrayToSplit = params[0];
  let column = params[1];
  Ember.Logger.info(arrayToSplit); //this prints an empty array
  var i;
  var col1 = [];
  var col2 = [];
  for(i=0; i<arrayToSplit.length; i+=1) {
      if(i%2==0) {
          col1.push(byTheNumbers[i]);
      } else {
          col2.push(byTheNumbers[i]);
      }
  }
  if (column == 1) {
    return col1;
  } else {
    return col2;
  }
}

export default Ember.Helper.helper(twoColumns);

Also if I do the following, all the elements in the array display fine: 另外,如果我执行以下操作,则数组中的所有元素都可以正常显示:

<div class="row">
<div class="small-12 columns">
{{#each numbers as |number|}}
    <h3><b>{{number.value}}</b></h3>
    <h6>{{number.title}}</h6>
{{/each}}
</div>

Yes i have done this way by using component, in component you can write these computed properties and use in component.hbs. 是的,我已经通过使用component来做到这一点,在component中,您可以编写这些计算出的属性并在component.hbs中使用。

evenItems: Ember.computed('numbers.[]',  function() {
     return this.get('numbers').filter(function(num){ return num.value % 2 === 0 })
 }),

 oddItems: Ember.computed('numbers.[]',  function(number) {
     return this.get('numbers').filter(function(num){ return num.value % 2 !== 0 ;})
 })

you can check here https://ember-twiddle.com/fc3e3e85c1a25fea6865d36822c45864?openFiles=components.even-odd.js%2Ctemplates.components.even-odd.hbs 您可以在这里查看https://ember-twiddle.com/fc3e3e85c1a25fea6865d36822c45864?openFiles=components.even-odd.js%2Ctemplates.components.even-odd.hbs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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