Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting hasMany relationships example #54

Open
denisnazarov opened this issue Aug 5, 2013 · 7 comments
Open

Persisting hasMany relationships example #54

denisnazarov opened this issue Aug 5, 2013 · 7 comments

Comments

@denisnazarov
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Author

@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
Copy link
Contributor

ghempton commented Aug 6, 2013

@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
Copy link
Author

@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
Copy link
Contributor

ghempton commented Aug 6, 2013

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants