0

I've been able to hack around the ember-data RESTAdapter and make it work for my django web app using the djangorestframework. It's clearly a different flavor of REST from what Rails has implemented.

I'm extending the original DS.RESTAdapter to work with the django approach and I'm curious how I can take the "record" that they typically turn into JSON and instead make a basic query dict of "foo=bar&baz="

Here is what i've done so far that does work -I'd just like a less hard coded approach

DS.RESTAdapter = DS.Adapter.extend({
  bulkCommit: false,

  createRecord: function(store, type, record) {
    var root = this.rootForType(type);

    //var data = {};                                                                             
    //data[root] = record.toJSON();
    var data = 'username=%@'.fmt(record.get('username'))
...
Toran Billups
  • 27,111
  • 40
  • 155
  • 268

1 Answers1

0

If you define your models like so:

MyApp.MyModel = DS.Model.extend({
    property: DS.attr('string'),
    ....
});

MyApp.MyModel.reopenClass({
  url: '/path?ids=%@
});

Then in your adapter:

find: function(store, type, id) {
    var url = type.url;
    url = url.fmt(id);
    ....
})
Joachim H. Skeie
  • 1,893
  • 17
  • 27
  • If possible I'd like to keep the query dict as data so I can do PUT/POST like normal – Toran Billups Sep 04 '12 at 11:41
  • Add a function on your Model class that will format the string you are after. Looking at what you are trying to do, it looks like you can format it in the same manner as you would format your query string. Have a look at the following question for JSON -> Query String formatting: http://stackoverflow.com/questions/3308846/serialize-json-to-query-string-in-javascript-jquery – Joachim H. Skeie Sep 04 '12 at 12:23
  • thanks for the post -the question you mention got me 90% of the way there w/ this jQuery helper => $.param(record.toJSON()); The only thing missing is that I need to exclude the id from post / put yet when I do the usual delete record['id'] or delete record.id it still remains ... (another question perhaps) – Toran Billups Sep 04 '12 at 16:31
  • I am not sure I follow your question regarding excluding the id... Why would you want to exclude the id ? – Joachim H. Skeie Sep 04 '12 at 16:36
  • It's just an opinion of the django rest framework that I'm using. the http POST to create a new "person" needs every attr except the id. And the PUT has the id in the uri so it doesn't need to be in the data that gets sent over the wire - http://stackoverflow.com/questions/12267796/how-to-exclude-an-objects-id-property-before-or-during-tojson – Toran Billups Sep 04 '12 at 16:37