Emberjs-route-qury-param

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

クエリパラメータを指定するルーター

URLにバインドできるルートドリブンコントローラーでクエリパラメーターを指定し、コントローラーでクエリパラメーターを宣言して、それらをアクティブにすることができます。 配列のクエリパラメーターフィルターの計算されたプロパティを定義することにより、テンプレートを表示できます。

構文

Ember.Controller.extend ({
   queryParams: ['queryParameter'],
   queryParameter: null
});

以下の例は、ルート駆動型コントローラーでのクエリパラメーターの指定を示しています。 新しいルートを作成し、_specifyquery_という名前を付け、_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('specifyquery');
});

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

次のコードで_app/templates/_の下に作成されたファイル_application.hbs_を開きます-

<h2>Specifying Query Parameters</h2>
{{#link-to 'specifyquery'}}Click Here{{/link-to}}

上記のリンクをクリックすると、ページがフォームで開きます。 SpecifyQuery.hbsファイルを開いて、ルート駆動型コントローラーにパラメーターを送信します-

//sending action to the addQuery  method
<form {{action "addQuery" on = "submit"}}>
   {{input value = queryParam}}
   <input type = "submit" value = "Send Value"/>
</form>
{{outlet}}

_specifyquery_テンプレートを表示する_queryParam_フィルターされた配列の計算されたプロパティを定義します-

import Ember from 'ember';

export default Ember.Controller.extend ({
  //specifying the 'query' as one of controller's query parameter
   queryParams: ['query'],

  //initialize the query value
   query: null,

  //defining a computed property queryParam
   queryParam: Ember.computed.oneWay('query'),
   actions: {
      addQuery: function () {

        //setting up the query parameters and displaying it
         this.set('query', this.get('queryParam'));
         document.write(this.get('query'));
      }
   }
});

出力

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

Ember.jsルータークエリパラメーター

あなたがリンクをクリックすると、それは値を入力するための入力ボックスを提供し、addQueryメソッドにアクションを送信します-

Ember.jsルータークエリパラメーター

ボタンをクリックすると、?の右側にキーと値のペアが表示されます。 URLで-

Ember.jsルータークエリパラメーター