Emberjs-route-map
提供:Dev Guides
ルーターがコントローラーのプロパティを別のクエリパラメーターキーにマップする
コントローラーには、クエリパラメーターキーをアタッチし、コントローラープロパティを別のクエリパラメーターキーにマップするデフォルトのクエリパラメータープロパティがあります。
構文
Ember.Controller.extend ({
queryParams: {
queryParamName: "Values"
},
queryParamName: null
});
例
以下の例は、コントローラーのプロパティを別のクエリパラメーターキーにマッピングする方法を示しています。 新しいルートを作成してparammapcontrolという名前を付け、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('parammapcontrol');
});
//It specifies Router variable available to other parts of the app
export default Router;
次のコードで_app/templates/_の下に作成されたファイル_application.hbs_ファイルを開きます-
<h2>Map a Controller's Property</h2>
{{#link-to 'parammapcontrol'}}Click Here{{/link-to}}
上記のリンクをクリックすると、ページが開き、ユーザーが入力した値を受け取る入力ボックスが表示されます。 _parammapcontrol.hbs_ファイルを開き、次のコードを追加します-
//sending action to the addQuery method
<form {{action "addQuery" on = "submit"}}>
{{input value = queryParam}}
<input type = "submit" value = "Send Value"/>
</form>
{{outlet}}
今、次のコードで_app/controllers/_の下に作成された_parammapcontrol.js_ファイルを開きます-
import Ember from 'ember';
export default Ember.Controller.extend ({
queryParams: [{
//mapping the string 'querystring' of the 'query's' query parameter
query: "querystring"
}],
//initialy query's 'query parameter' will be null
query: null,
queryParam: Ember.computed.oneWay('query'),
actions: {
addQuery: function () {
this.set('query', this.get('queryParam'));
document.write(this.get('query'));
}
}
});
出力
エンバーサーバーを実行すると、次の出力が表示されます-
リンクをクリックすると、値を入力できる入力ボックスが生成されます。 これは、addQueryメソッドにアクションを送信します-
ボタンをクリックすると、「?」の右側にパラメータ値が表示されますURLで-