npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-query-params-service

v1.0.0

Published

Prototype Service for managing query params via a service

Downloads

1,281

Readme

ember-query-params-service

Build Status Maintainability Ember Observer Score npm

Compatibility

  • Ember.js v3.13 or above
  • Ember CLI v3.13 or above

Embroider support is available at ember 4.4 and higher

Installation

ember install ember-query-params-service

DISCLAIMER

This package is a work in progress and while it provides a more ergonomic way to access query params from anywhere in the app, there is still a dependency on controllers if you want to be able to link to routes with query params. This is due to an allow-list that's implemented in the route resolver.

RFC here: https://github.com/emberjs/rfcs/pull/380

Also, this is an experiment, and SemVer guarantees cannot be assured (yet).

Usage

The @queryParam Decorator

Signature:

@queryParam: void;
@queryParam(param: string);
@queryParam(param: string, options: TransformOptions);
@queryParam(options: TransformOptions);

TransformOptions<T> = {
  defaultValue?: any;
  deserialize?: (param: string) => T;
  serialize?: (value: T) => string;
}

example:

import Route from '@ember/routing/route';
import { queryParam } from 'ember-query-params-service';

export default class ApplicationRoute extends Route {
  @queryParam isSpeakerNotes;
  @queryParam slideNumber;

  model() {
    return {
      isSpeakerNotes: this.isSpeakerNotes,
      slideNumber: this.slideNumber
    }
  }
}
{{this.model.isSpeakerNotes}} - {{this.model.slideNumber}}

optionally, an options hash may be passed to the decorator defining any of the three options, defaultValue, serialize, deserialize.

An example of using the default value would be:

import Component from "@glimmer/component";
import { queryParam } from "ember-query-params-service";

export default class SomeComponent extends Component {
  @queryParam({
    defaultValue: false,
  })
  active;

}

When the value is undefined, the defaultValue will be used when accessing the value

When the value is undefined or equal to the defaultValue, the param will not be present on the URL. In this way the URL will only contain params that are different than the default value.

An example of using serialize / deserialize would be:

import Component from "@glimmer/component";
import { queryParam } from "ember-query-params-service";

export default class SomeComponent extends Component {
  @queryParam({
    deserialize: (qp) =>  parseInt(( qp || '' ).replace(/-/g, '')),
    serialize: (value) => `-${value}-`,
  })
  foo;

   addToFoo() {
    this.foo = (this.foo || 0) + 1;
  }
}

this would not only allow numeric operations on the query param (whereas, by default, query params are all strings), but it also allows any sort of transform to occur between the queryParam in the URL and the property that you want to interact with.

Expanded usage with the service

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';


export default class ApplicationRoute extends Route {
  @service queryParams;

  @alias('queryParams.current.r') isSpeakerNotes;
  @alias('queryParams.current.slide') slideNumber;

  model() {
    return {
      isSpeakerNotes: this.isSpeakerNotes,
      slideNumber: this.slideNumber
    }
  }
}

Setting Query Params

  • Directly on the service:

      @service queryParams;
    
      // ...somewhere
      this.queryParams.current.queryParamName = 'some value';

    and then the URL will show queryParamName=some%20value

  • or via the @alias decorator:

      @alias('queryParams.current.r') isSpeakerNotes;
    
      // ...somewhere
      this.isSpeakerNotes = false;

    and then the URL will show r=false

  • or via the @queryParam decorator:

      @queryParam isSpeakerNotes;
    
      // ...somewhere
      this.isSpeakerNotes = false;

    and then the URL will show isSpeakerNotes=false

  • or via the @queryParam decorator with renaming the param in the URL

      @queryParam('r') isSpeakerNotes;
    
      // ...somewhere
      this.isSpeakerNotes = false;

    and then the URL will show r=false

API

  • queryParams.current - the current set of query params for the currentURL

  • queryParams.byPath - query params for every route that has been visited since the last refresh

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.