Emberjs-route-desrlztn
提供:Dev Guides
ルーターのデフォルト値と逆シリアル化
値がURLにシリアル化されないコントローラークエリパラメータープロパティの既定値を設定できます。
構文
Ember.ArrayController.extend ({
queryParams: 'queryParameterName',
queryParameterName: defaultValue
});
例
次の例では、クエリパラメータにデフォルト値を設定することを指定しています。 新しいルートを作成し、_defaultvaluedeserialize_という名前を付け、_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('defaultvaluedeserialize');
});
//It specifies Router variable available to other parts of the app
export default Router;
次のコードで_app/templates/_の下に作成されたファイル_application.hbs_ファイルを開きます-
<h2>Default Values and Deserialization</h2>
{{#link-to 'defaultvaluedeserialize'}}Click Here{{/link-to}}
上記のリンクをクリックすると、ページが開き、ユーザーが入力した値を受け取る入力ボックスが表示されます。 _defaultvaluedeserialize.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/_の下に作成された_defaultvaluedeserialize.js_ファイルを開きます-
import Ember from 'ember';
export default Ember.Controller.extend ({
//assigning query parameter name as 'query'
queryParams: 'query',
//assigning the query param to a default value as 1
query: 1,
queryParam: Ember.computed.oneWay('query'),
actions: {
addQuery: function () {
this.set('query', this.get('queryParam'));
document.write(this.get('query'));
}
}
});
出力
エンバーサーバーを実行すると、次の出力が表示されます-
リンクをクリックすると、指定されたデフォルト値が入力ボックスに表示されます-
ボタンをクリックすると、デフォルト値が表示され、URLにシリアル化されません-