ember.js - How can I peek the contents of a hasMany relationship? -


i have model, node:

app.node = ds.model.extend({     parents: ds.hasmany('node', { inverse: null }) }); 

let's server sending partial tree:

{ id: "a", parents: [ "c" ] } { id: "b", parents: [ "c", "d" ] } { id: "c", parents: [ "e" ] } 

i want render so:

a ---- c       / b ---/ 

however, when call b.get('parents'), message stating:

uncaught error: assertion failed: looked 'parents' relationship on 'node' id b of associated records not loaded. either make sure loaded parent record, or specify relationship async (ds.hasmany({ async: true }))

neither of desirable me. want loaded records in graph.

i may want render well:

a ---- c--?       / b ---/--? 

representing unimportant parents ui element.

is there way peek loaded records in relationship?

great question.

currently, ember data falls short on such use cases.

there an answer question it's obsolete now: api has changed , it's private.

if you're brave enough, can this:

app.node = ds.model.extend({   parent:   ds.belongsto('node', {inverse: 'children'}),   children: ds.hasmany('node', {inverse: 'parent'}),    childrenids: ember.computed(     '_internalmodel._relationships.initializedrelationships.children.canonicalstate.@each.ids',     function() {       var children =                   ._internalmodel           ._relationships           .initializedrelationships           .children;        if (!children) return [];        return children         .canonicalstate         .mapby('id');     }   ),    availablechildren: ember.computed('childrenids', function () {     var childrenids = this.get('childrenids');     return this.store.all('node').filter(function(node) {       return childrenids.indexof(node.id) > -1;     });   }) }); 

demo: http://emberjs.jsbin.com/qugofu/1/edit?html,js,output

the problem code (apart using private api) availablechildren property not auto-update when new nodes fetched server.

you'll have figure out way it. when do, release addon!

upd 2015-07-20

this question wasn't letting me sleep.

i've managed improve above solution:

  1. it automatically updates when new records appear in store.
  2. it updates when relationships of records change.

the downside lookup available nodes happens in component , not in model.

demo: http://emberjs.jsbin.com/qugofu/2/edit?html,js,output


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -