dgeb/ember_data_example

issues with including/not including root in json

Closed this issue · 2 comments

If you include the root 'contact' in your json, then the initial load goes all 'null null' for me. If you don't however the ember-data bit that does the json[root] lookup is incorrect and didCreateRecord seems to throw an error. Trying to work through this now, but wanted to document in case I'm doing it wrong or it's a known bug.

So if I add the patch below to ember-data-extensions.js things work again. If other parts of the ember code are assuming a root, shouldn't this as well?

diff --git a/app/assets/javascripts/lib/ember-data-extensions.js b/app/assets/javascripts/lib/ember-data-extensions.js
index 71520b5..f653e25 100644
--- a/app/assets/javascripts/lib/ember-data-extensions.js
+++ b/app/assets/javascripts/lib/ember-data-extensions.js
@@ -3,7 +3,11 @@ DS.Store.reopen({
var array = DS.ModelArray.create({ type: type, content: Em.A([]), store: this });
this.registerModelArray(array, type);

  • this.loadArray(type, data);
  • var object_array = [];
  • for (var i = 0; i < data.length; i++) {
  •  object_array.push(data[i].contact);
    
  • }
  • this.loadArray(type, object_array);
dgeb commented

@jackdempsey - thanks for bringing this up. I hadn't looked at this buggy example for a while.

The default rest adapter does seem to expect a json root element for most actions. The following simple commit resolved many obvious problems:

b955dcf

I'm going to leave loadAll() as-is for now because it's consistent with loadMany(), which expects a data array without a root element. You can see that the adapter immediately strips the root element when passing data to the store:

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/vendor/ember-data.js#L226

I would say that loadAll() belongs in ember-data, so I'll submit a PR and force a decision about this.

Thanks again. I'll try to wrap up this example soon...