vendredi 2 février 2018

Can't access Ember's class variable

How to properly pass a variable to an Ember's class?

Controller:

import Controller from '@ember/controller';
import Object from '@ember/object';

function totalVotes(company) {
  return company.upvotes + company.downvotes;
}

function calcPercent(company) {
    return (company.upvotes * 100 / (company.upvotes + company.downvotes)).toFixed(2);
}

function percentComparator(a, b) {
    return calcPercent(b) - calcPercent(a);
}

var Company = Object.extend({
  score: function() {
    return (this.get('upvotes') * 100 / totalVotes(this)).toFixed(2);
  }.property('upvotes', 'downvotes')
});

var AppModel = Object.extend({
  topCompanies: function() {
    return this.get('companies')
      .sort(percentComparator)
      .slice(0, 8);
  }.property('companies.@each.upvotes', 'companies.@each.downvotes'),
});

var appModel = AppModel.create({
  companies: getCompaniesJSON().map(function(json) {
    return Company.create(json);
  })
});

export default Controller.extend({
  topCompanies: appModel.topCompanies,
});

Template:

<ul>

  <li> %</li>

</ul>

The result of the above in the browser console:

jquery.js:3827 Uncaught TypeError: Cannot read property 'sort' of undefined

this.get('companies') is undefined. Why? I'm passing companies to AppModel.create. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire