EmberJS:对象作为刷新模型的查询参数 [英] EmberJS: Object as Query Param to Refresh Model

查看:88
本文介绍了EmberJS:对象作为刷新模型的查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循查询参数指南( http://guides.emberjs。 com / v1.11.0 / routing / query-params / ),它的效果很好。具体来说,刷新模型完全是我想要的。

I followed the Query Params guide (http://guides.emberjs.com/v1.11.0/routing/query-params/) and it worked great. Specifically, refreshing the model did exactly what I wanted.

我将过滤器移动到json-api规范,过滤发生在过滤器对象。所以不是:

I'm moving the filter to the json-api spec and filtering takes place in a filter object. So rather than:

http://localhost:3000/accounts?id=1

服务器响应:

http://localhost:3000/accounts?filter[id]=1

我试图获取查询参数可以基于一个对象来刷新模型,但是似乎没有更新。

I tried to get the query params to work refreshing the model based on an object, but it doesn't seem to update.

// app/controllers/accounts/index.js

import Ember from 'ember';

export default Ember.Controller.extend({
  queryParams: ['filter', 'sort'],

  filter: {},
  sort: '-id'
});


// app/routes/accounts/index.js
import Ember from 'ember';

export default Ember.Route.extend({
  queryParams: {
    filter: { refreshModel: true },
    sort: { refreshModel: true }
  },
  model: function(params) {
    return this.store.find('account', params);
  },
});

// template
<th>{{input type="text" placeholder="ID" value=filter.id}}</th>

可以让查询参数与对象一起使用吗?

Is it possible to have query params work with an object?

推荐答案

这个答案是Ember版本 1.13.0-beta.1 + canary / em>

简短的答案:否。查询参数不能与对象一起工作。

长的答案:

截至目前,$ $中的私人函数名为 _serializeQueryParams c $ c> Router 序列化 queryParams

As of now, a private function named _serializeQueryParams in the Router serializes the queryParams.

_serializeQueryParams(targetRouteName, queryParams) {
  var groupedByUrlKey = {};

  forEachQueryParam(this, targetRouteName, queryParams, function(key, value, qp) {
    var urlKey = qp.urlKey;
    if (!groupedByUrlKey[urlKey]) {
      groupedByUrlKey[urlKey] = [];
    }
    groupedByUrlKey[urlKey].push({
      qp: qp,
      value: value
    });
    delete queryParams[key];
  });

  for (var key in groupedByUrlKey) {
    var qps = groupedByUrlKey[key];
    var qp = qps[0].qp;
    queryParams[qp.urlKey] = qp.route.serializeQueryParam(qps[0].value, qp.urlKey, qp.type);
  }
},

qp.urlKey 将在您的示例中评估为'filter',对象将被序列化为'object [Object]'。即使您可以覆盖路由中的 serializeQueryParam 方法,因为 queryParam 键仍然是'filter',你需要它是'filter%5Bid%5D'

qp.urlKey would evaluate to 'filter' in your example, and object would be serialized as 'object [Object]'. Even though you could override the serializeQueryParam method in your route, that wouldn't help because the queryParam key would still be 'filter', and you'd need it to be 'filter%5Bid%5D'

根据这个评论在Ember讨论论坛,听起来像对象查询参数是不太可能的,你最好只是平坦化和不平坦化过滤的字段。

Based on this comment in the Ember Discussion Forum, it sounds like object query params are unlikely, and you'd be better off just flattening and unflattening the filtered fields.

这篇关于EmberJS:对象作为刷新模型的查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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