mercredi 27 mai 2015

Creating POST Request with non-standard json using Ember-Data

I'm trying to create a post request with Ember Data. In my Giraffe controller, I try to create a record like this.

 var giraffe = this.store.createRecord('giraffe', {
        d: new Date(),
        type: "giraffe"                                        
        });
giraffe.save();

In my models/giraffe.js file, I have this

import DS from 'ember-data';

export default DS.Model.extend({
    d: DS.attr('date'),
    type: DS.attr('string')

});

I am using a key value store for my backend where records are stored according to date. I only need to send a POST request to the backend with the object for the server to store it. Copying some code from Discoruse, in my GiraffeAdapter, I did createRecord like this

  createRecord(store, type, attrs) {
        const data = {}; //copied from discourse
        const typeField = Ember.String.underscore(type); copied from discourse
        console.log(typeField, "typeField");
        data[typeField] = attrs;

        Ember.$.ajax('https://localhost:8080/api/', {  
             type: 'POST',
             dataType: "json",
             data: data,
             success: function(data, response) {
                 console.log(data, response);
             },
             error: function (xhr) {
                 console.log('error')
             }
        });    
    },

this is giving me the following error

Uncaught TypeError: str.replace is not a function

str.replace is used in the Ember.string.underscore method that I copied from discourse and which is defined here

http://ift.tt/1Bp6hWt

var UNDERSCORE_CACHE = new Cache(1000, function(str) {
  return str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2').
    replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase();
});

In addition to the str.replace error, i'm not sure if I am going about this correctly (i.e. in my attempt to store non-standard json using a POST request).




Aucun commentaire:

Enregistrer un commentaire