Emberjs-route-sticky-param

提供:Dev Guides
移動先:案内検索

EmberJS-ルータースティッキクエリパラメータ値

Emberでは、クエリパラメータ値はデフォルトでスティッキーです。クエリパラメータに変更が加えられると、ルートを再入力してもクエリパラメータの新しい値が保持されます。

構文

Ember.Controller.extend ({
   queryParams: ['paramValue'],
   paramValue:true/false
});

以下の例では、スティッキクエリパラメータ値の使用を指定しています。 新しいルートを作成し、_stickyqueryparam_という名前を付け、_router.js_ファイルを開いてURLマッピングを定義します-

import Ember from 'ember';
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config

//The const declares read only variable
const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});

//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
   this.route('stickyqueryparam');
});

//It specifies Router variable available to other parts of the app
export default Router;

次のコードでapp/templates/の下に作成されたファイルapplication.hbsを開きます-

<h2>Sticky Query Param Values</h2>
{{#link-to 'stickyqueryparam'}}Click here to open the page{{/link-to}}

上記のリンクをクリックすると、スティッキクエリパラメータテンプレートページが開きます。 stickyqueryparam.hbsファイルには次のコードが含まれています-

<h2>My Page</h2>
{{link-to 'Show' (query-params showThing=true)}}
{{link-to 'Hide' (query-params showThing=false)}}
<br>
{{#if showThing}}
   <b>Welcome to finddevguides..</b>
{{/if}}
{{outlet}}

次のコードでapp/controllers/の下に作成されたstickyqueryparam.jsファイルを開きます-

import Ember from 'ember';

export default Ember.Controller.extend ({
   queryParams: ['showThing'],
  //showThing would be false, if only the route's model is changing
   showThing: false
});

出力

エンバーサーバーを実行すると、次の出力が表示されます-

Ember.js Router Sticky Query Param

リンクをクリックすると、_Show_および_Hide_リンクを提供して、スティッキクエリパラメータテンプレートページが開きます-

Ember.js Sticky Query Param

_Show_リンクをクリックすると、テキストが表示され、_Hide_リンクはテキストを非表示にします-

Ember.js Router Sticky Query Param