Emberjs-route-opt

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

完全な移行を選択するルーター

refreshModel configプロパティをtrueに設定することにより、コントローラーのクエリパラメータープロパティが完全な遷移にオプトするように変更されたときに、オプションのqueryParams構成を使用できます。 _transitionTo_または_link-to_引数は、クエリパラメーター値で変更されますが、ルート階層では変更されません。コントローラープロパティは、URL内の新しいクエリパラメーター値で更新されます。

構文

Ember.Route.extend ({
   queryParams: {
      queryParameterName: {
         refreshModel: true
      }
   }
});

以下の例は、コントローラークエリのparamプロパティが変更されたときに完全な遷移を選択することを示しています。 新しいルートを作成し、_paramfulltrans_という名前を付け、_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('paramfulltrans');
});

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

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

<h2>Opting Into a Full Transition</h2>
{{#link-to 'paramfulltrans'}}Click Here{{/link-to}}

上記のリンクをクリックすると、ページが開き、ユーザーが入力した値を受け取る入力ボックスが表示されます。 _queryParams_構成を使用して、_paramfulltrans.hbs_ファイルを開いて完全な遷移を選択します-

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

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

import Ember from 'ember';

export default Ember.Controller.extend ({
  //specifying '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 the query parameters and displaying it
         this.set('query', this.get('queryParam'));
         document.write(this.get('query'));
      }
   }
});

次に、それぞれのコントローラーでRouteの_queryParams_構成を使用し、app/routes/_の下で定義された_paramfulltrans.js_ファイルで_refreshModel configプロパティをtrueに設定します。

import Ember from 'ember';

export default Ember.Route.extend ({
   queryParams: {
      query: {
        //opting into full transition
         refreshModel: true
      }
   }
});

出力

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

Ember.jsルーターが完全な移行を選択

リンクをクリックすると、値を入力してアクションをaddQueryメソッドに送信できる入力ボックスが生成されます-

Ember.jsルーターが完全な移行を選択

ボタンをクリックすると、「?」の右側にパラメータ値が表示されます "URLで-

Ember.jsルーターが完全な移行を選択