ember-data不要在url中查询参数

时间:2013-11-24 11:55:04

标签: parsing rest ember.js ember-data

https://parse.com/docs/rest#queries-relational

    curl -X GET \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -G \
  --data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' \

这是其余网址

  

https://api.parse.com/1/classes/GameScore

如何在ember rest api中实现这一目标

这是我更新标题的方式,但我不知道其他内容在哪里

 DS.RESTAdapter.reopen({
    headers: {
      "X-Parse-Application-Id": "xxxxxxxxxxxxxxxxx",
      "X-Parse-REST-API-Key": "xxxxxxxxxxxxxxxxK",
    }
  });

2 个答案:

答案 0 :(得分:1)

您可能需要创建自己的REST adapter版本。

var MyAdapter = DS.RESTAdapter.extend({
  extraOpts: null,

  ajaxOptions: function(url, type, hash) {
    this._super();

    if (this.extraOpts !== undefined) {
      // modify the hash for use by jQuery.ajax()
    }
  }
});

答案 1 :(得分:0)

很难从头开始实现适配器。我建议您使用https://github.com/clintjhill/ember-parse-adapter

但是如果你还想做,这是一个初步的实现:

Javascript

App = Ember.Application.create({});

App.ParseSerializer = DS.RESTSerializer.extend({
    // tell to ember data that objectId is the id in parse api
    primaryKey: 'objectId',
    // transform { results: [...] } to { game_score: [...] }
    normalizePayload: function(primaryType, payload) {                
        payload[primaryType.typeKey] = payload.results;        
        delete payload.results;
        return  payload;
    }
});

App.ParseAdapter = DS.RESTAdapter.extend({
    host: 'https://api.parse.com',
    namespace: '1/classes',
    headers: {
        "X-Parse-Application-Id": "xxx",
        "X-Parse-REST-API-Key": "xxx"
    },
    // when building the url we need https://api.parse.com/1/classes/GameScore
    // instead of https://api.parse.com/1/classes/game_score
    pathForType: function(type) {        
        return Ember.String.classify(type);
    },
    // this is the trick to becomce find('game_score', { foo: "bar" }) in where={"foo":"bar"}
    findQuery: function(store, type, query) {
        return this.ajax(this.buildURL(type.typeKey), 'GET', { data: { where: JSON.stringify(query) } });
    }
})

App.ApplicationSerializer = App.ParseSerializer;
App.ApplicationAdapter = App.ParseAdapter;

App.IndexRoute = Ember.Route.extend({
  model: function() {
      return this.store.find('game_score', { "playerName":"Sean Plott", "cheatMode":false });
  }
});

App.GameScore = DS.Model.extend({
    score: DS.attr('number'),
    playerName: DS.attr('string'),
    cheatMode: DS.attr('string')
    // other properties
});

<强>模板

<script type="text/x-handlebars" data-template-name="application">
    <h1>ember-latest jsfiddle</h1>
    {{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
  <h2>Index Content:</h2>
  <ul>
  {{#each}}
      <li>{{playerName}}</li>
  {{/each}}
   </ul>
</script>