ember.js - Ember Data: Automatically set belongsTo association -
i using ember data 1.0 (beta 1) , confused default behavior of associations in ed.
assume book
model has many (hasmany
) chapters
each chapter
belonging (belongsto
) book. expectation instances of book's chapters
property automatically have reference book instance through belongsto
association, appears not case. is indeed default behavior in ember data or overlooking something?
if indeed default behavior, mean need create custom serializer accomplish this?
no ember-data not should possible achieve. in both cases ember-data sideload properties. (in past versions setup mapping embedded, no longer can this) in example have following:
app.book = ds.model.extend({ chapters: ds.hasmany('chapter') }); app.chapter= ds.model.extend({ book: ds.belongsto('book') });
once setup ember-data default data structured this:
{ "book": { "id": "1" "chapters": ["1", "2", "3"] }, "chapters": [ { "id": "1", "book": "1" }, { "id": "2", "book": "1" }, { "id": "3", "book": "1" } ] }
if data not in format , can not change can extend extractsingle or extractarray methods on serializer type. @ bottom of link can find more info on that. remeber looks in camelcase may need normalize json object well.
Comments
Post a Comment