samedi 16 janvier 2016

Ember find existing hasMany models

How can I get (or rather check for the existence of) the sub-model objects that belong to a parent model in Ember-Data (v1.13.15) without throwing errors?

I have associated models looking like this:

// models/parent.js
...
export default DS.Model.extend({
    children: DS.hasMany('child', {async:true, inverse:'parent'}),
...
});


// models/child.js
...
export default DS.Model.extend({
    parent: DS.belongsTo('parent', {async:true, inverse:'children'}),
....
});

In my route's model hook I'm trying to check if I have any children already associated with my parent (parent btw is also a submodel of a hasMany/belongsTo of a 'family' model...). If I haven't added any children yet, I need to create some and associate them to the parent object, but when I do something like:

parent.get('children').then(function(children){
    // would be nice to get in here and maybe have children.length() == 0
});

I get Uncaught (in promise) TypeError: Cannot read property 'then' of undefined. Similarly can't call parent.get('children').get('length'). this.store.findAll('child').then... also seems to error out.

Is it not possible to get an empty array or do I have to use some dirty catch to figure out if I have any submodels? I'm pretty inexperienced with Ember so any pointers or help would be appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire