2
votes

Am using ember-cli with handlebars template for my ember js application.I have the requirement to pass value from hanldebars template to ember controller's computed property.Is there any way ember supports to pass parameters or any value from handlebars template to ember controller's computed property. Thank You

I want do something like:

App.Person = Ember.Object.extend({

 hasPermission: function(value1, value2) {
 // setter
  if(value1 === value2){
    return "You are permitted";
  }else{
    return "You are not permitted";
  }

 }.property('hasPermission')
});

And from handlebars template:

{{ hasPermission value1 value2 }}

Something like it

2
Please provide a specific, concrete example involving code if at all possible. What is the computed property? What property(s) is/are it based on? What parameter are you trying to pass? To have a specific example helps everyone understand what you are thinking and if computed properties are even a good match for what you're trying to accomplish. (I expect not.)Chris Peters

2 Answers

16
votes

The short answer is that you can't. Computed properties in Ember are not functions. Ember has some syntactic sugar to make them easier to declare using functions, but they're not functions. You can think of computed properties in Ember just like computed properties in Javascript: they look like functions, but they're not. You can't pass in arguments to a computed property because it's not a function.

That being said, I think what you want is a Handlebars helper. You can pass in properties from your controller and perform computations on them to get a result. From your example:

Ember.Handlebars.helper('hasPermission', function(value1, value2) {
    if (value1 === value2) {
        return 'You are permitted';
    } else {
        return 'You are not permitted';
    }
});

You can use that helper exactly how you have it in your question:

{{hasPermission value1 value2}}
1
votes

To build on @GJK's answer above, you could do this with computed properties, but you'll need the other properties on the model as well:

App.Person = Ember.Object.extend({
  value1: 'foo',
  value2: 'bar',
  hasPermission: function() {
    if (this.get('value1') === this.get('value2') {
      return "You are permitted";
    } else {
      return "You are not permitted";
    }
  }.property('value1', 'value2')
});

This code doesn't make a whole lot of sense, though, as permissions aren't controlled in models, they should be handled elsewhere.