Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters between routes

Tags:

ember.js

What is the "appropriate" way in Ember to send a parameter from one route to another? For instance, I have two routes defined as such:

this.resource('activities', { path: '/activities/:on_date' }, function() {
    this.route('new');
});

when on the ActivitiesRoute the user is presented with a dropdown of possible activities. When they choose something it transitions to the ActivitiesNewRoute:

this.transitionToRoute('activities.new');

and I know there is a second parameter available in the transitionToRoute(route,model) method but it's meant for passing in a model and I'm assuming this shouldn't be repurposed for other parameter passing. In this case the dropdown choice is picking an Action model id and the model for ActivitiesNew is a Activity.

Here are my three guesses at ways that might work:

1) Make it a router parameter

I supposed I could change ActivitiesNew to include a "parameter" as part of the route:

this.route('new', { path: '/new/:my_parameter' });

I'm not sure I'd really like to have it becoming part of the URL path but if this was the prevailing convention then I'd live with that.

2) Get a handle, post transition

Immediately following the transitionToRoute call I could set a property of the new controller class. Not sure if the controller would be setup yet but I'm imagining something like:

this.transitionToRoute('activities.new');
this.get('target').controllerFor('activities.new').set('my_parameter', myValue);

3) Use model parameter

this.transitionToRoute('activities.new',myValue);

I suspect that this is a major no-no. I haven't looked into the Ember code to know if this could work but it seems against convention so this is my "bad option".

like image 576
ken Avatar asked Sep 30 '13 16:09

ken


People also ask

How do you pass multiple route parameters in a react URL path?

To add multiple parameters with React Router, we can get the URL parameters from the useParams hook. <Route path="/:category/:id" exact component={ItemDetails} />; to add the category and id parameters to the Route . We call useParams to return an object with the id and category parameters.

How do you pass parameters in URL in react?

Using React Router, when you want to create a Route that uses a URL parameter, you do so by including a : in front of the value you pass to Route 's path prop. Finally, to access the value of the URL parameter from inside of the component that is rendered by React Router, you can use React Router's useParams Hook.

What is @param in angular?

A collection of matrix and query URL parameters.


2 Answers

transitionTo & transitionToRoute return a "promise-like" object. The parameter this object is resolved with is the route, from which you can access controller and currentModel. So a nice clean way to pass information to a route to which you are transitioning is:

var my_param = ....;
this.transitionToRoute('activities.new').then(function(newRoute) {
  newRoute.currentModel.set('someProperty', my_param);
  //or
  newRoute.controller.set('someProperty', my_param);
});

EDIT/RANT:

note that in most cases, you do want to use needs, and bind things between controllers. However, there are certainly instances when you have things that depend on the logic of a route transition -- eg., controllerB has state X if we came to routeA from routeB, but state Y if we came from routeC. In that case, my answer is valuable.

The primary value of stack overflow to the development community is not the immediate answers you get to questions you post, but the massive ever growing wealth of googleable development knowledge. When you "infer" from a user's question that they "should" be doing something other than what they are asking how to do, you may be right (or you may be just incapable of imagining their particular circumstance), but if you answer only with your recommendation/rule/aphorism/cargo-cult-dictum instead of answering the ACTUAL QUESTION, you diminish the value of everybody else's google searches. If you want to tell someone to do something other than what they're asking, do it in a comment, or in a footnote to an answer to the actual question.

like image 170
Michael Johnston Avatar answered Nov 14 '22 06:11

Michael Johnston


You can use the needs API (Read about it here):

App.ActivitiesNewController = Ember.ObjectController.extend({
  needs: ['activities']

  // Bind the property you need
  actionTemplateBinding: 'controllers.activities.actionTemplate'
});

So what you actually need is to pass a parameter between controllers, which is exactly what needs is for. Plus, binding the property with needs you ensure it is in sync at all times, instead of relying on setupController being called.

like image 28
Panagiotis Panagi Avatar answered Nov 14 '22 06:11

Panagiotis Panagi