0
votes

I thought this would be super simple but its turning into a pain in the butt.

I need to create a function that will accept a string and return it re-formatted as a URL slug. For example I'd pass in the string "The Adventures of Huckleberry Finn" and my function would return the string "the_adventures_of_huckleberry_finn". I have no problem doing the actual conversion. However in Ember actions can only return True, False or Undefined so the action always returns an object named Undefined rather than the string I told it to return.

How can I go about creating this function/action/computed property in my controller so that it correctly returns the formatted string? I have attempted to just add a normal JS function into the controller. I also tried to define this as a computed property but this action will be used only in the controller, the template has no need to know anything about the slug so a computed property dosn't really make sense and I couldn't get it to work anyways.

2

2 Answers

1
votes

Ok, here is an example in how to achieve that: jsfiddle.net/NQKvy/837

0
votes

I would return a computed property call something like slug

slug: function(){
 return this.get('title').split(' ').join('_');
}.property('title')

Then you can use {{slug}} in your template of this.get('slug') in your controller.

Cheers