1

Currently experimenting with Ember.js and loving it so far. The biggest pain point I have run across is dealing with complex forms that control multiple models. In Rails this is a fairly straightforwad process.

# Models
class Person < ActiveRecord::Base
  attr_accessible :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :person
end

# View
<%= form_for @person do |f| %>
  <%= f.text_field :name %>
  <%= f.fields_for :addresses do |af| %>
    <%= af.text_field :street %>
  <% end%>
<% end %>

With Ember, I already know how to nest routes and represent multiple objects with one route. What I cannot figure out is how to actually manipulate those models using one form, while still respecting any relevant validation logic, binding and dirty-checking. Any pointers?

EDIT: This seems to be a real issue with Ember Data, so it looks like I will have to figure out how to do this with plain Ember objects.

Community
  • 1
  • 1
Steven Garcia
  • 2,446
  • 2
  • 19
  • 12

1 Answers1

1

You can still work things out with ember data. In nested model dirty checking shouldn't be that hard, you would have to loop through the nested model and check if they in-turn are dirty using observers(which is not that difficult). You can even do bulk saves on nested model which I didn't know was possible before : What Does bulkCommit Mean In The Context Of Ember's RestAdapter?.

As for nested json structure, it can be bit tricky. But we have been able to work with nested JSON structure using custom transforms: https://github.com/emberjs/data/blob/master/TRANSITION.md#json-transforms

Having observers to track if transformed data is dirty can be bit overwhelming, but you can easily add a function that checks if the data has gone dirty in the transformation itself.

Community
  • 1
  • 1
Deewendra Shrestha
  • 2,054
  • 1
  • 20
  • 47
  • Thank you for the concise and very helpful answer. Still coming to grips with the wide surface area of Ember's API, had not run across either of those functions. I'd love to see a simple demo of how you accomplish this task with JSON transforms. The docs aren't exactly explicit in this regard. – Steven Garcia Apr 03 '14 at 00:45
  • Here is the link to the transform i wrote: http://stackoverflow.com/questions/20549321/how-can-we-perform-rollback-on-custom-transformed-data. There you can see i have rollback action baked in the transform, similarly you can add a function to check if the raw transform was dirty. You would have to check if the model is dirty and also the raw transform is dirty separately. But before going there, if possible you should always use model instead. We just had a scenario where model didnt make sense because there was not specific structure in which the data is returned from the server. – Deewendra Shrestha Apr 03 '14 at 01:46