如何使用ember-data将查询参数传递给后端?

时间:2022-03-02 19:42:10

I'm trying to wrangle my way through my first Ember.js app, and am struggling to figure out how to take my boilerplate/tutorial-based code and get it to pass parameters through to my Flask backend.

我正在试图通过我的第一个Ember.js应用程序,并努力弄清楚如何采用我的样板/基于教程的代码并让它将参数传递到我的Flask后端。

I want to serve up local events based on zip code, with the zip code being passed through to the backend.

我希望根据邮政编码提供本地活动,邮政编码将传递到后端。

I came across this page on Opting into a Full Transition (http://emberjs.com/guides/routing/query-params/#toc_opting-into-a-full-transition) and am now seeing the parameter name but not the value coming through the request to the API.

我遇到了关于选择完全转换的页面(http://emberjs.com/guides/routing/query-params/#toc_opting-into-a-full-transition),我现在看到的是参数名称而不是值通过API请求。

I visit this URL in my browser: http://localhost:5000/?zip=21210

我在浏览器中访问此URL:http:// localhost:5000 /?zip = 21210

and I get:

我得到:

127.0.0.1 - - [22/Dec/2014 23:20:51] "GET /?zip=21210 HTTP/1.1" 200 - # first call to load page
127.0.0.1 - - [22/Dec/2014 23:20:52] "GET /api/v1/events?zip= HTTP/1.1" 200 - # call to API

my app.js file:

我的app.js文件:

App = Ember.Application.create();

App.Router.map(function() {
    location: 'auto'
  // put your routes here
  //this.resource('events', {path: '/'})
});

//var attr = DS.attr;

DS.RESTAdapter.reopen({
  namespace: 'api/v1'
});

App.Event = DS.Model.extend({
    name: DS.attr('string'),
    address: DS.attr('string')
});


App.IndexRoute = Ember.Route.extend({
    queryParams: {
        zip: {
            refreshModel: true
        }
    },
  model: function(params) {
    return this.store.findQuery('event', params);
  }
});

App.IndexController = Ember.ArrayController.extend({
    queryParams: ['zip'],
    zip: null
});

1 个解决方案

#1


1  

queryParams binds zip to the property zip in your controller which is null. This is why you are seeing an empty zip parameter being passed in to the server.

queryParams将zip绑定到控制器中的属性zip,该zip为null。这就是您看到传递给服务器的空zip参数的原因。

Make sure to assign a non-null value to zip property and you will stop seeing zip parameter being empty.

确保为zip属性指定一个非空值,并且您将停止看到zip参数为空。

#1


1  

queryParams binds zip to the property zip in your controller which is null. This is why you are seeing an empty zip parameter being passed in to the server.

queryParams将zip绑定到控制器中的属性zip,该zip为null。这就是您看到传递给服务器的空zip参数的原因。

Make sure to assign a non-null value to zip property and you will stop seeing zip parameter being empty.

确保为zip属性指定一个非空值,并且您将停止看到zip参数为空。