Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get url params in emberjs component

Tags:

ember.js

I am beginner at emberjs, I want to access url params in component but don't know how it is done in ember js.

I could get url params in route with code below:

model(params) {
    this.store.query('ticket', {
        page: {
            number: params.page,
            size: params.size
        }
    });
},

queryParams: {
    page: {
        refreshModel: true
    },
    size: {
        refreshModel: true
    }
}
like image 302
Prakash Avatar asked May 17 '16 06:05

Prakash


People also ask

How do we get the params from the URL?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.

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.

How do you read a query parameter in Ember?

EmberJS - Query ParametersQuery 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.

How do I get query params Express?

Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.


1 Answers

The quick answer:

You can inject the router into the component where you need it like this:

import Ember from 'ember';

const { computed: { alias }, observer } = Ember

export default Ember.Component.extend({
  routing: Ember.inject.service('-routing'),
  params: alias('routing.router.currentState.routerJsState.fullQueryParams')
})

Here is a twiddle with this implemented.

The longer answer:

You're using the injecting routing which is private, and using routerJsState.fullQueryParams like that is not really standard practice.

Because a query param is really a property somewhere, if you can make that "somewhere" a service, it would be better, because then you can inject that service into your component instead of routing like we do now. For example page and limit can live on a service and be injected into a controller and component, and would be a nicer solution.

Here is that solution in a twiddle.

The relevant code:

services/task-pager.js
import Ember from 'ember';

export default Ember.Service.extend({
 page: 0,
 limit: 3,
 nextPage: function() {
    this.set('page',this.get('page') + 1)
    console.log(this.get('page'))
  },
  previousPage: function() {
    this.set('page',this.get('page') - 1)
  }
})

// controllers/tasks.js
export default Ember.Controller.extend({

 taskPager: Ember.inject.service(),

 queryParams:['page', 'limit'],

 page: Ember.computed.alias('taskPager.page'),

 limit: Ember.computed.alias('taskPager.limit'),

 actions: {
   nextPage: function() {
     this.get('taskPager').nextPage()
   },
   previousPage: function() {
     this.get('taskPager').previousPage()
   }
 }
})

//components/display-qp.js
export default Ember.Component.extend({
  params: Ember.inject.service('task-pager')
})

//templates/components/display-qp.hbs
COMPONENT PARAMS PAGE:{{params.page}}
like image 110
TameBadger Avatar answered Sep 30 '22 14:09

TameBadger