将控制器属性公开给路由的模型函数

时间:2020-12-10 21:10:35

I've defined some properties in a controller for a pagination:

我在控制器中为分页定义了一些属性:

import Ember from 'ember';

export default Ember.ArrayController.extend({
  limit: 1,
  skip: 0,
  pageSize: 1
  }
});

I'd like to access limit in the Route's model-function but I don't know how.

我想访问Route的模型函数中的限制,但我不知道如何。

import Ember from 'ember';

export default Ember.Route.extend({

   model: function(params) {

      console.log(this.get('controller').get('limit')) <- doesnt work for example
      return this.store.find('post', {limit: 1,
                                      sort:'createdAt desc'});
   }
});

1 个解决方案

#1


1  

Maybe you should take a look at the queryParams option (http://emberjs.com/guides/routing/query-params/).

也许您应该查看queryParams选项(http://emberjs.com/guides/routing/query-params/)。

With query params you can set the limit to be a query param in your URL like http://yourdomain.com/someroute?limit=15.

使用查询参数,您可以将限制设置为URL中的查询参数,例如http://yourdomain.com/someroute?limit=15。

Your controller will become:

你的控制器将成为:

export default Ember.ArrayController.extend({
    queryParams: ['limit'],  // Here you define your query params
    limit: 1                 // The default value to use for the query param
});

You route will become:

您的路线将成为:

export default Ember.Route.extend({
    model: function(params) {
        return this.store.find('post', {
            limit: params.limit, // 'limit' param is available in params
            sort:'createdAt desc'
        });
    }
});

Alternative:

替代方案:

If you don't want to use query params, another solution might be to define the limit property in one of the parent route's controller. By doing so you can access the property in the model hook by doing:

如果您不想使用查询参数,则另一种解决方案可能是在父路由的控制器之一中定义limit属性。通过这样做,您可以通过执行以下操作来访问模型钩子中的属性:

this.controllerFor('parentRoute').get('limit');

#1


1  

Maybe you should take a look at the queryParams option (http://emberjs.com/guides/routing/query-params/).

也许您应该查看queryParams选项(http://emberjs.com/guides/routing/query-params/)。

With query params you can set the limit to be a query param in your URL like http://yourdomain.com/someroute?limit=15.

使用查询参数,您可以将限制设置为URL中的查询参数,例如http://yourdomain.com/someroute?limit=15。

Your controller will become:

你的控制器将成为:

export default Ember.ArrayController.extend({
    queryParams: ['limit'],  // Here you define your query params
    limit: 1                 // The default value to use for the query param
});

You route will become:

您的路线将成为:

export default Ember.Route.extend({
    model: function(params) {
        return this.store.find('post', {
            limit: params.limit, // 'limit' param is available in params
            sort:'createdAt desc'
        });
    }
});

Alternative:

替代方案:

If you don't want to use query params, another solution might be to define the limit property in one of the parent route's controller. By doing so you can access the property in the model hook by doing:

如果您不想使用查询参数,则另一种解决方案可能是在父路由的控制器之一中定义limit属性。通过这样做,您可以通过执行以下操作来访问模型钩子中的属性:

this.controllerFor('parentRoute').get('limit');