String interpolation in handlebars emberjs template

I am using http://ivaynberg.github.io/select2/ to provide autocomplete functionality in rails-emberjs app. I have some_dynamic_id value. My rudder code looks like this:

{{
  view App.TokenAutoCompleteView                                                                                               
  searchUrl="/users/" + some_dynamic_id + "/books/search"
  optionValuePath="content.id"
  optionLabelPath="content.name"
  multiple="false"
}}

      

I want to assign a dynamically generated url to the searchUrl attribute. I tried to use bind-attr from Ember Interpolation on href tag in handlebars template but couldn't get it to work.

+3


source to share


1 answer


Remember Handlebars templates are logicless, so there is no concept of string interpolation. You have to do this in a computed property inside your controller (or component).

In your controller:

dynamicSearchUrl: function() {
    return '/users/' + this.get('someDynamicId') + '/books/search';
}.property('someDynamicId')

      

In your template:

{{view App.TokenAutoCompleteView searchUrl=dynamicSearchUrl ... }}

      




Update: It's been a long time since I answered this, so I figured I'd update. Ember.js now uses a newer version of Handlebars that supports subexpressions . This means that string concatenation in templates is now possible. You just need to write a concatenation helper.

// helpers/concat.js
export default Ember.Helper.helper(function(first, second, three) {
    // You could make this apply to more than three strings...
    return first + '' + second + '' + three;
});

      

Once you've done that, just use a subexpression in your template:

{{
  view App.TokenAutoCompleteView
  searchUrl=(concat "/users/" some_dynamic_id "/books/search")
  optionValuePath="content.id"
  optionLabelPath="content.name"
  multiple="false"
}}

      

I still prefer my first answer in this situation, but the second is now possible.

+8


source







All Articles