Pregunta

I have a function that does what I want it to do using lodash and vanilla javascript. I pass it a value, it checks to see if that value is already mapped, and then it checks to see if the value has specific child properties. If it does, it passes the the child property through the same function.

I'm trying to do essentially the same thing in an ember app, but the function is a property in a controller. When I try to reference the function name, I get an error: "Uncaught ReferenceError: collectHeaders is not a function". How can I pass the result of "collectHeaders(child)" back to the function?

Just lodash & javascript where it works:

function collectHeaders(parent) {    
  var firstChild = parent.children ? parent.children[0] : {};

  if (firstChild.section_type && firstChild.section_value) {
    _.forEach(parent.children, function(child) {
      collectHeaders(child)
    });
  } else if (parent.children) {
    // more code
  }
}

Ember.app controller with lodash:

var mapping = {};

var CompareController = Ember.ObjectController.extend({

collectHeaders: function(parent) {
    var firstChild = parent.children ? parent.children[0] : {};

    if (firstChild.section_type && firstChild.section_value) {
      _.forEach(parent.children, function(child) {
        collectHeaders(child);
      });
    } else if (parent.children) {
      // more code
    }
  },
});

export default CompareController;
¿Fue útil?

Solución

Your code is out of scope, your previous function was global

collectHeaders: function(parent) {
    var self = this;
    var firstChild = parent.children ? parent.children[0] : {};

    if (firstChild.section_type && firstChild.section_value) {
      _.forEach(parent.children, function(child) {
        self.collectHeaders(child);
      });
    } else if (parent.children) {
      // more code
    }
  },
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top