Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the params in controller ember.js

Tags:

ember.js

This is my router.js code.

this.route('contact',{'path': '/contact/:chat_id'});

and This is my route.js code.

model(params) {

  return this.store.findRecord("chat", params.chat_id)
},

and This is my controller.js code and can i use like this. It shows error as null value please help me. How to use params in controller

recordChat: function() {

  var chat = this.get(params.chat_id)

  Ember.RSVP.hash({
    offer_id: chat,
  })
}
like image 459
Girja Shanker Avatar asked Jan 09 '17 12:01

Girja Shanker


People also ask

How do I find Route parameters and query strings?

import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.

What is the use of query params?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed.

Which parameters appear to the right of the in a URL it is represented as optional key value pairs 1 point A values B State C full transition d query?

EmberJS - Query Parameters Query parameters are specified on route driven controllers which appear to the right of the ? in a URL and are represented as optional key-value pairs. The above URL has the two query parameters; one is sort and the other is page which contains values ASC and 2 respectively.

What is Route query?

The Route Query tool allows you to check where packets with a certain IP address are routed according to the current definitions. You can check that the routing is correct and quickly find a particular branch in the Routing tree.


2 Answers

Edited for 2021: There's actually probably a much simpler way to do this now in Ember. See https://guides.emberjs.com/v3.27.0/routing/query-params/

Original Answer

I think the simplest answer is to create a variable in route and then set it in the setupController:

your_route.js

export default Ember.Route.extend({
  model(params) {
    this.set('myParam',params.my_Params);
    
    // Do Model Stuff...
  },
  setupController(controller, model) {
    // Call _super for default behavior
    this._super(controller, model);
    // Implement your custom setup after
    controller.set('myParam', this.get('myParam'));
  }
});
like image 138
Cameron Avatar answered Oct 21 '22 12:10

Cameron


In your route.js, you need to call the setupController function like this:

setupController(controller, model) {
    this._super(...arguments);
    controller.set('chat', model);
}

Now you have access to it in your controller by calling:

recordChat() {
   const chat = this.get('chat');
   //  if you don't call the setupController function, you could also do:
   // const chat = this.get('model') or this.get('model.id') if you want the id only
}

UPDATE:

See working twiddle here

like image 39
Remi Smirra Avatar answered Oct 21 '22 12:10

Remi Smirra