emberjs / data

WarpDrive is a lightweight data library for web apps — universal, typed, reactive, and ready to scale.
https://api.emberjs.com/ember-data/release
MIT License
3.04k stars 1.33k forks source link

HasMany serialization not working for me using pushObject #3507

Closed brendanoh closed 9 years ago

brendanoh commented 9 years ago

I am on Ember 1.13.3 and ED 1.13.4 and it seems that my DS.hasMay relationships are not getting serialized.

I get the feeling that ED external API has changed in some way I am missing.

stefanpenner commented 9 years ago

I am not ready to go to glimmer but there is no clear cut way to determine a working ED version.

please report issues that prevent you from upgrading to ember 1.13.x – these seem of upmost importance.

So frustrating.

I am sure you are, just so you are aware this comment doesn't provide constructive value. Remember we are all in this together.

I get the feeling that ED is somehow broken or at least incompatible as every pattern I have tried before still gets me the same issue.

it may be useful to provide an example of every (or maybe just the expedcted on) pattern you have tried. To give potential reviewers a leg up on finding a solution. Ideally an example app/jsbin demonstrating the issue.

brendanoh commented 9 years ago

The massive upheaval in ED is probably good and I am not trying to be an ass although in my frustration it probably comes too easy. I apologize for coming off like a dick.

I tried to do this

 campaign.get('campaignChannels').pushObject(fs.channel);

When that didnt work I was told in latest ED I should do this:

 campaign.get('campaignChannels').pushObject(fs.channel_internalModel);

So it started to die here:

var assertPolymorphicType = function (record, relationshipMeta, addedRecord) {
  var addedType = addedRecord.type.modelName;

addedRecord is undefined

So I commented out the assert and now I see that in relationships.js

 addRecord: function (record, idx) {

The record is undefined.

So I am way down the rabbit hole which I am sure is one of my own making but I can't figure out how/why.

sly7-7 commented 9 years ago

@brendanoh I'm almost sure you have to give a DS.Model instance to a hasMany.pushObject. That beeing said, if your relationship is define as async, then I think you should use the pushObject when the relationships resolves, something like (considering fs.channel is a DS.Model instance):

campaign.get('campaignChannels').then(function(channels) {
channels.pushObject(fs.channel);
}):

Hope this helps

jcope2013 commented 9 years ago

call .toArray() on channels before pushing object https://github.com/emberjs/data/issues/3313

brendanoh commented 9 years ago

@sly7-7 I tried this both ways. Async with the pushObject after it resolves and not async. Neither worked.

@jcope2013 will try now.

Thanks all

sly7-7 commented 9 years ago

@brendanoh When you say

@sly7-7 I tried this both ways. Async with the pushObject after it resolves and not async.

Does it throws an error ? Or is it just that this results in your hasMany not beeing serialized ? In the latter case, you should now that in case of a one-to-many relationship, the hasMany is not serialized by default.

brendanoh commented 9 years ago

@sly7-7 Uh oh. I meant it isnt serialized.

If it isnt serialized by default then what changed in ED as I have never had to do anything before like this.

Changing default behavior one beta before "release" has made this a difficult upgrade to say the least.

brendanoh commented 9 years ago

So I reverted to Ember 1.12.1 and ED beta 18. And it still doesnt serialize into my API call.

I am going crazy.

stefanpenner commented 9 years ago

not constructive but, couldn't resist: https://www.youtube.com/watch?v=pygwaX2VNzQ

stefanpenner commented 9 years ago

warning if you let youtube autoplay subsequent videos, you will actually go insane

sly7-7 commented 9 years ago

@stefanpenner :joy_cat: @brendanoh At least we are going forward :smile: In your case, I think your either could use the EmbeddedRecordMixin to serialize the hasMany relation inside the parent, or explicitely define your relation, in the serializer as: {serialize: true} see this test for an example of setting it: https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/serializers/json-serializer-test.js#L362

iamjstates commented 9 years ago

@brendanoh - did you find a fix. I'm having the same error.

Thanks

sly7-7 commented 9 years ago

@iamjstates Did you try the above suggestion ?

iamjstates commented 9 years ago

@sly7-7 how do I set serialize every HasMany - this is a PITA.

sly7-7 commented 9 years ago

Ok, in this case, in your ApplicationSerializer you could simply override the https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json-serializer.js#L806 method to return true, and it should work.

@wecc @igorT Maybe we could consider exposing this method as public for override ?

wecc commented 9 years ago

Should be fine overriding the private method for now.

brendanoh commented 9 years ago

@iamjstates not for current version of ED. In my previous project we were using RestAdapter and I never had this issue. On this new project I didn't even realize was using ActiveModelAdapter so I had to do this for every model with hasMany's:

import DS from 'ember-data';

export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    campaignChannels: { serialize: 'ids' },
    distributions: { serialize: 'ids' },
    goals: { serialize: 'ids' }
  }
});

Still confused on how I am going to be able to upgrade since the API has change so much. I love the ED project and appreciate all the contributors immensely but I feel like the deadline to hit semvar alignment with Ember made the 18-19-1.13 path all the more painful.

sly7-7 commented 9 years ago

@brendanoh I've recently the app I'm working on to ED 1.13, and it was working very well for me (only warning messages as far as I remember). Ember migration was harder. I would be happy to help you to migrate if you want. Ping me on slack :)

brendanoh commented 9 years ago

Thanks @sly7-7 I will do that :+1:

zyllorion commented 9 years ago

Was there a resolution to this? Similar issues, no matter if I use push, pushObject, addObject ...

sly7-7 commented 9 years ago

Could you provide more context, a stacktrace, failing test ? At least could you describe what is wrong (is it an error or just the hasMany not beeing serialized). Did you configure you serializer correctly ? (which serializer). It's hard to help you with so little information.

vvscode commented 8 years ago

I have the same issue <DS.AdapterPopulatedRecordArray:ember4106> and try to add object into

Stacktrace stack trace

at initSelection I have next code

      options.initSelection = function (element, callback) {
        var value = element.val() || get(self, 'value'),
            selection = get(self, 'selection'),
            content = new A(get(self, '_content')), // <DS.AdapterPopulatedRecordArray:ember4106>
            multiple = get(self, 'multiple');

        if (!value || !value.length) {
          // set "value" and "selection" if they are empty and "prompt" is unset
          if (content && get(content, 'length') && !get(self, 'prompt') && !get(self, 'promptPath') && !get(self, 'multiple')) {
            self.dataToSelection(get(content, 'firstObject'));
          }
          return callback([]);
        }
        /**
         * Make sure content always contains selection
         */
        if (selection) {
          if (selection instanceof Array) {
            content.addObjects(selection);
          } else {
            content.addObject(selection);
          }
        }
vvscode commented 8 years ago

@sly7-7 @zyllorion Ember 1.12.1 Ember Data 1.13.15

also the same error if I try convert content with .toArray()

brendanoh commented 8 years ago

@vvscode This wont be much help but the only way I got around this was to have Ember and Ember Data versions that "matched". By that I mean ones that were obviously compatible once ED went gold.

I was on 1.12.1 and tried every version of Ember Data after beta 18 and none worked. Hacks and workarounds were unsustainable.

So I eventually moved my whole App to recent versions (Ember was hard but ED was easy BTW) and now everything works seamlessly and as expected. I am currently on Ember 1.13.12 & Ember Data 1.13.15.