user1629165
user1629165

Reputation: 81

using port with Ember Data RESTAdapter

I modified the bloggr-client sample app to read from a rest interface at localhost:8080/api

App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.RESTAdapter.extend({
    url: 'localhost:8080/api';})    });

I'm also using

<script src="js/libs/jquery-1.9.1.js"></script> 
<script src="js/libs/handlebars-1.0.0.js"></script>
<script src="js/libs/ember-1.0.0-rc.7.js"></script>
<script src="js/libs/ember-data-master.js"></script>

In firefox, I get the following javascript error:

line 113 in ember-1.0.0.rc.7.js // When using new Error, we can't do the arguments check for Chrome. Alternatives are welcome try { fail.fail(); } catch (e) { error = e; }

In Chrome,

There adapter returns no data but I get an options error on line 8526 in jQuery

here is the results of the query. Perhaps the backslashes or quotes are killing the adapter. Can I fix this with the serializer?

query: localhost:8080/api/posts

"[{\"user\": \"joe\", \"update_time\": \"2013-08-25 23:15:03.011767\", \"create_time\":        \"2013-08-25 23:05:45.405645\", \"slug\": \"apple\", \"title\": \"apple\"}, {\"user\": \"bill\", \"update_time\": \"2013-08-26 00:38:36.713749\", \"create_time\": \"2013-08-26 00:37:04.935824\", \"slug\": \"orange-id\", \"title\": \"orange\"}]"

Late breaking news, This fixed the error message. I'm now getting data back from the server with backslashes but ember data is ignoring it. Can the serializer fix this?

App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.RESTAdapter.extend({
    bulkCommit: false, 
    url: "http://localhost:8080/api/medbook",
    primary_key: "user",
    ajax: function (url, type, hash) {
        hash.url = url;
        hash.type = type;
        hash.dataType = 'jsonp';
        hash.contentType = 'application/json; charset=utf-8';
        hash.context = this;

        if (hash.data && type !== 'GET') {
            hash.data = JSON.stringify(hash.data);
        }

        jQuery.ajax(hash);
        },
  })
});

Upvotes: 0

Views: 1030

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

It does not seem like a problem with the serializer. The custom adapter code you've included does not define a success or failure property on the hash passed to jQuery.ajax. As a result the request is made to the server but it's response is ignored.

Your custom adapter will need an ajax method that works like the built in one - use that code as a starting point then customize as needed.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L475

  ajax: function(url, type, hash) {
    var adapter = this;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      hash = hash || {};
      hash.url = url;
      hash.type = type;
      hash.dataType = 'json';
      hash.context = adapter;

      if (hash.data && type !== 'GET') {
        hash.contentType = 'application/json; charset=utf-8';
        hash.data = JSON.stringify(hash.data);
      }

      if (adapter.headers !== undefined) {
        var headers = adapter.headers;
        hash.beforeSend = function (xhr) {
          Ember.keys(headers).forEach(function(key) {
            xhr.setRequestHeader(key, headers[key]);
          });
        };
      }

      hash.success = function(json) {
        Ember.run(null, resolve, json);
      };

      hash.error = function(jqXHR, textStatus, errorThrown) {
        if (jqXHR) {
          jqXHR.then = null;
        }

        Ember.run(null, reject, jqXHR);
      };

      Ember.$.ajax(hash);
    });
  },

Upvotes: 0

Related Questions