0

I want to make an API call for searching that looks like this:

https://myapi.com/search/<query>/<token>

where query is the search term and token (optional) is an alphanumeric set of characters which identifies the position of my latest batch of results, which is used for infinite scrolling.

This call returns the following JSON response:

{
  "meta": { ... },
  "results" {
    "token": "125fwegg3t32",
    "content": [
      {
        "id": "125125122778",
        "text": "Lorem ipsum...",
        ...
      },
      {
        "id": "125125122778",
        "text": "Dolor sit amet...",
        ...
      },
      ...
    ]
  }
}

content is an array of (embedded) items that I'm displaying as search results. My models look like this:

App.Content = Em.Model.extend({
  id: Em.attr(),
  text: Em.attr(),
  ...
});

App.Results = Em.Model.extend({
  token: Em.attr(),
  content: Em.hasMany('App.Content', {
    key: 'content',
    embedded: true
  })
});

In order to make that API call, I figured I have to do something like this:

App.Results.reopenClass({
  adapter: Em.RESTAdapter.create({
    findQuery: function(klass, records, params) {
      var self = this,
          url = this.buildURL(klass) + '/' + params.query;

      if (params.token) {
        url += '/' + params.token;
      }

      return this.ajax(url).then(function(data) {
        self.didFindQuery(klass, records, params, data);
        return records;
      });
    }
  }),
  url: 'https://myapi.com/search',
});

then somewhere in my routes do this:

App.Results.fetch({query: 'query', token: '12kgkj398512j'}).then(function(data) {
  // do something
  return data;
})

but because the API returns a single object and Em.RESTAdapter.findQuery expects an array, an error occurs when Ember Model tries to materialize the data. So how do I do this properly? I'm using the latest build of Ember Model.

By the way, I'm aware that it would be much more convenient if the API was designed in a way so I can just call App.Content.fetch(<object>), which would return a similar JSON response, but I would then be able to set the collectionKey option to content and my data would be properly materialized.


Short delay when trying to run redirect with ember route with firebase authentication

Alright, this is my first question on SO so I'll try to make it a good one sorry ahead of time.

I've been using ember-cli to work on a product. I am using Firebase simple login for authentication and have created an initializer that adds an auth object to all my controllers and routes.

This works, but... There seems to be a delay for the redirect checking. example I go to /secret and this should redirect me back to the /login route and it does but there is a slight delay where I can see the template

I've tried to create a gist with all the necessary information. Let me know if there is anything else I can provide to help out

https://gist.github.com/mbertino/060e96e532f8ce05d2d0

4

1 回答 1

0

您只需要覆盖您的模型load()方法即可将有效负载哈希调整为 Ember.Model 想要的。Ember.Model 中没有序列化程序。既有用于处理集合的类级负载,也有用于加载特定于单个模型的 JSON 的实例级负载。您希望覆盖实例级加载方法以将 content键值包装在一个数组中(如果它还没有的话)。

我一直在大量使用 Ember.Mode,并针对我的一些用例对其进行了增强,并提交了 PR 以进行修复和增强。这些 PR 已经坐在那里一段时间了,维护者没有回应。我现在已经转移到 Ember.Data,可以说它已经“重新启动”并且现在有了更好的结果。

我强烈建议离开 Ember.Model,因为 Ember Data 采取的新务实方向似乎已经死了,而且项目维护者似乎不再对它感兴趣。

于 2014-09-15T21:44:38.563 回答