Rigel
Rigel

Reputation: 882

ember-data model not getting serialized different host

Ember 1.7.0 Ember Data 1.0.0 - beta 10

I have a product model which fetch data from different rails server My adapter are

import DS from 'ember-data';
export default DS.RESTAdapter.extend({
    namespace: 'api/v1'
});

and for products

import ApplicationAdapter from './application';
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
    namespace: 'api/v1',
    host: 'http://localhost:4000' 
});

my product model is

import DS from 'ember-data';

export default DS.Model.extend({
  itemId: DS.attr('string'), //this is NOT a primary key 
  title: DS.attr('string'),
  thumbnailUrl: DS.attr('string'),
  categories: DS.attr('string'),
  currency: DS.attr('string'),
  price: DS.attr('number'),
  productUrl: DS.attr('string'),
  pictureUrl: DS.attr('string')
});

the json response from my other rails server is

{
    "products": [{
        "id": "1",
        "item_id": "310351720028",
        "title": "some cool title",
        "thumbnail_url": "http://mythumbnail.url",
        "categories": "abc",
        "currency": "$",
        "price": "6900.0",
        "product_url": "some url",
        "picture_url": "hereis m"
    }, {
        "id": "2",
        "item_id": "221588471947",
        "title": "Title 1",
        "thumbnail_url": "http://mythubnail",
        "categories": "def",
        "currency": "$",
        "price": "449.0",
        "product_url": "someurl",
        "picture_url": "someurl"
    }]
}

the attributes with snake case: _ are not displayed they are undefined example item_id, thumbnail_url.

I tried making a serailizer myself using ember g serializer Product

import DS from 'ember-data';
import Ember from 'ember';
export default DS.RESTSerializer.extend({
    keyForAttribute: function(attr) {
        return Ember.String.camelize(attr);
    }
});

//when i do a console.log(Ember.String.camelize(attr)) i see that snake case is getting converted to camelCase. I dont have a products route

but no luck...

UPDATED:

This should work to for RESTAdapter as per solution on irc by @chrism_ http://jsbin.com/ripaqe/3/edit?html,js,output

Upvotes: 0

Views: 215

Answers (1)

givanse
givanse

Reputation: 14943

Because you are using JSON with snake_case, instead of using a DS.RESTSerializer use an DS.ActiveModelSerializer. It is designed specifically for this cases, it will save you work.

See: http://emberjs.com/api/data/classes/DS.ActiveModelSerializer.html

Upvotes: 2

Related Questions