queryParams中的嵌套对象 [英] Nested object in queryParams

查看:770
本文介绍了queryParams中的嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何传递类型为

query: {
  id: bla,
  email: bla-bla-bla
}

在queryParams?

in queryParams?

这样的简单方式

App.ArticlesController = Ember.ArrayController.extend({
  queryParams: ['query'],
  query: {
    id: bla,
    email: bla-bla-bla
  }
});

当然不行:)

PS对不起,英文不好:)

P.S. Sorry for bad english :)

推荐答案

Ember Query Parameters不支持将对象设置为QueryParam。它只支持键值对。根据文档

Ember Query Parameters doesn't support setting an object as a QueryParam. It only supports key-value pairs. According to the documentation:

查询参数是可选的键值对,它们出现在右边的
上?在一个URL例如,以下URL具有两个查询
参数,排序和页面,各自的值为ASC和2:

Query parameters are optional key-value pairs that appear to the right of the ? in a URL. For example, the following URL has two query params, sort and page, with respective values ASC and 2:

如果您正在寻找将 id 电子邮件属性绑定到querystring vals,您必须指定它显式地:

If you're looking to bind both the id and email properties to the querystring vals, you'll have to specify it explicitly:

App.ArticlesController = Ember.ArrayController.extend({
  queryParams: ['id, email'],
  id: null,
  email: null
});

如果你真的想绑定 id 电子邮件直接连接到查询对象,您可以设置观察者或计算属性(取决于您的目标)任何 id 电子邮件属性中的任何更改,然后更新您本地拥有的其他对象。

If you really want to bind the id and email directly to a query object, you could setup an observer or computed property (depending on your goal) that observes a change in any of id or email properties and then updates some other object you have locally.

如果要简化此设置,可以考虑使用Controller Mixin来定义queryParams中的一组常用属性以及相应的数组。那么你可以做一些例子:

If you want to streamline this setup, you could consider using a Controller Mixin that defines a common set of properties in your queryParams plus a corresponding array. Then you'd be able to do something like:

App.ArticlesController = Ember.ArrayController.extend( MyQueryParamsMixin, {
  queryParams: myQueryParams
  // The other properties would be set explicitly from your Mixin
  // Technically, you don't even need to declare the properties since they'll get bound anyway
  // thanks to queryParams, but it's good practice to let readers of your Mixin know which properties
  // are in use.
});

这篇关于queryParams中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆