getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

Persisting hasMany relationships example #54

Open denisnazarov opened 11 years ago

denisnazarov commented 11 years ago

I am having trouble persisting a hasMany relationship and wanted for someone to provide a non-trivial example.

My models:

App.Release = Ep.Model.extend
    name: Ep.attr('string')

App.TestCase = Ep.Model.extend
    name: Ep.attr('string')
    description: Ep.attr('string')

App.Release.reopen
  releases: Ep.hasMany(Pixelmark.Release) 
  test_cases: Ep.hasMany(Pixelmark.TestCase)

App.TestCase.reopen
  releases: Ep.hasMany(Pixelmark.Release)

Say I have a bunch of TestCases that already exist. Some are assigned to a Release, some are not, and none of them are newly created models on the client. Say I have two model instances, release, and test_case. To assign a test case to a release, I am trying to do release.get('test_cases').addObject(test_case)

If I flush the session, nothing happens because nothing gets dirtied in the process. What is the proper way to persist this relationship? My models are sideloaded and not embedded, but do have a reference to the ids (the api returns an array of test_case_ids). I also have no problem reading the data (everything populates correctly with the sideloading), just persisting it back.

In think this would be very useful example for people to see.

kiwiupover commented 11 years ago

I have ported the ember_data_example to use EPF epf_example.

Here is the (Contacts New Controller](https://github.com/kiwiupover/epf_example/blob/master/app/assets/javascripts/controllers/contacts_new_controller.js)

  addPhoneNumber: function() {
    contact = this.get('model');
    contact.session.add(App.PhoneNumber.create({contact: contact}))
  },
denisnazarov commented 11 years ago

I think it works in your example because you are always creating new models in each case, which are automatically dirty. In my example, I am using existing models and simply reassigning them to different parents, they never get deleted. I assume because they are not "new", they don't get picked up as dirty and nothing persists.

Edit: My relationships are also sideloaded, and not embedded which may also pose a problem.

denisnazarov commented 11 years ago

@kiwiupover You also have a One-to-Many relationship, while I have a Many-to-Many. Are there any examples for persisting many-to-many records, specifically reassigning already existing models? As I said before, using addObject to the hasMany collection doesn't dirty the parent model so no PUT request happens.

ghempton commented 11 years ago

@denisnazarov, what does your backend look like? Is there an implicit join table? I still need to invest a little time in getting many to many relationships to work. For the very short term you could create an intermediate join object, but I would like to get many to many to work asap.

denisnazarov commented 11 years ago

@ghempton The backend is mongodb and the JSON returned has the data sideloaded. I think the easiest API is is just adding models to the hasMany collection, and it should just work when flushing. What do you think?

Let me know how I can help going forward with this.

ghempton commented 11 years ago

That seems like it should work. Would it save both sides of the collection? Similar to one to one, there probably should be the notion of one side "owning" the relationship?

denisnazarov commented 11 years ago

I think just one side should be saved, and the backend should know to update the other side.