9

Hi i am very new to ember js. i pass action parameters(id ) on link-to action in template but i did not get the values in my controller.

My Template code as follows:

index.html:

             <script type="text/x-handlebars" data-template-name="search">

             {{#each model.results}}

             // here i pass id value along with action

             {{#link-to 'profile' id action="profileinfo"}}

             </script>

app.js:

          App.SearchController = Ember.ObjectController.extend({

             id: '',

             actions:{
               profileinfo: function(id){

                // Here i access id value like this
               console.log(id);
               var id = this.get('id');})  

when i click on the link action goes to Searchcontroller, but i get id value is empty.I follow some solutions in stack overflow but unfortunately i did not get anything. Please provide some solution

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
vinay kallepalli
  • 231
  • 3
  • 11
  • You have given the python tag for this even though it is quite clearly not python – user1474424 Apr 03 '14 at 04:21
  • 1
    possible duplicate of [Combine linkTo and action helpers in Ember.js](http://stackoverflow.com/questions/16124381/combine-linkto-and-action-helpers-in-ember-js) – givanse Sep 26 '14 at 00:22

2 Answers2

5

I don't get why you're using the {{#link-to}} helper for triggering an action on your controller. Maybe you could simply use the {{action}} helper ?

If you try doing it that way, would it work ?

<button type="button" {{action "profileinfo" id}}>Click me !</button>

From there, your console.log(id); should get your value.

EDIT

Would also work for a <a> tag

<a href="#" {{action "profileinfo" id}}>Click me !</a>
Pascal Boutin
  • 1,220
  • 2
  • 9
  • 22
  • 1
    I can think of a scenario where you would want a `link-to` and also an `action`: say you have a button that you'd like to take the `active` state (which you get if it's a `link-to`) and you'd like the fact that the button was pressed to bubble up to a parent controller so that something can happen in the parent view (which normally wouldn't change if you were just routing to a child route in an outlet). – neverfox Jun 06 '14 at 04:37
  • 1
    I would still prefer to use a proper action in the model, that will then call a `this.transitionToRoute`. I basically think that `{{#link-to}}` must be restricted to basics redirections. If you want to want to handle a more complex logic, you gotta give this to `view` or even `controller` – Pascal Boutin Jun 06 '14 at 18:53
  • @PascalBoutin: I don't think that transitions should be triggered outside of routes, to ensure separation of concerns. The router routes, the controller processes actions ... Besides, as @neverfox mentions, `link-to` offers much more than simple redirection. There is an addon that extends link-to with action processing capabilities (https://github.com/Kuzirashi/ember-link-action), but it is not working for me right now (ember@2.2.0) – blueFast Dec 22 '15 at 16:22
  • @delavnog Hey my addon should definitely work for you, I have updated it to Ember 2.8 but it's working even for 1.13. Please try again and create issue if something is wrong still. :) – Daniel Kmak Aug 06 '16 at 14:18
0

I've created popular addon for doing just that:

{{#link-to 'profile' id invokeAction='profileinfo'}}

Simply install it:

ember installember-link-action

Please leave a star if you enjoy it or leave feedback if you feel anything is missing. :) It works with Ember 1.13 and 2.X (tested on Travis CI).

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89