【问题标题】:ember-data dont want query parameter in urlember-data 不需要 url 中的查询参数
【发布时间】:2013-11-24 11:55:03
【问题描述】:

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",
    }
  });

【问题讨论】:

    标签: parsing rest ember.js ember-data


    【解决方案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()
        }
      }
    });
    

    【讨论】:

    • 感谢这样的事情对我有用var ListAdapter = DS.RESTAdapter.extend({ findQuery: function(store, type, query) { return this.ajax(this.buildURL(type.typeKey), 'POST', { data: query }); }, ajaxOptions: function(url, type, hash) { this._super(url, type, hash); hash.processData = false; return hash; } }); 我正在努力将 JSON 字符串化为 stringfy 哈希,然后再发布将尝试弄清楚
    【解决方案2】:

    很难从头开始实现适配器。我建议你使用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多