当前位置: 首页 > 文档资料 > EmberJS 入门教程 >

选择全面过渡(Opting Into a Full Transition)

优质
小牛编辑
123浏览
2023-12-01

通过将refreshModel config属性设置为true,可以在控制器查询参数属性更改为选择完全转换时使用可选的queryParams配置。 transitionTolink-to参数将在查询参数值中更改,但不会在路由层次结构中更改; 控制器属性将使用URL中的新查询参数值进行更新。

语法 (Syntax)

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

例子 (Example)

下面给出的示例显示了当控制器查询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}}

现在定义queryParam过滤数组的计算属性,该数组将显示paramfulltrans模板 -

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
      }
   }
});

输出 (Output)

运行ember服务器,您将收到以下输出 -

Ember.js路由器选择全面过渡

当您单击该链接时,它将生成一个输入框,您可以在其中输入值并向addQuery方法发送操作 -

Ember.js路由器选择全面过渡

单击按钮后,它将在URL中的“?”右侧显示参数值 -

Ember.js路由器选择全面过渡