Skip to content

Instantly share code, notes, and snippets.

@thomasqbrady
Created November 27, 2013 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasqbrady/7684611 to your computer and use it in GitHub Desktop.
Save thomasqbrady/7684611 to your computer and use it in GitHub Desktop.
Trying to understand how to introspect hasMany related models in Ember/Ember Data
var doc = DS.Model.extend({
// relationships
metadata: DS.hasMany('metadatum',{ async: true }),
...................
var Metadatum = DS.Model.extend({
// relationships
doc: DS.belongsTo('doc'),
...................
Then in a contoller whose content is an Ember Data Store full of docs:
this.get('content').forEach(function(doc){
console.log(doc.get('metadata'));// NULL
});
@thomasqbrady
Copy link
Author

Would also love to know if there was a way to use this.store.filter('doc',{ metadata : { property: "match" } } ) or similar

@thomasqbrady
Copy link
Author

What I have done for now is make my own list with map:

currentCollectionSet: function() {
    var results = [];
            var slug = "test";
    var self = this;
        matches = this.store.filter('metadatum',function(md){
            return md.get('slug') == slug
        });
        var mdID = matches.get('content')[0].get('id');
        var docsPromise = this.store.find('doc');
        docsPromise.then(function(gdocs){
            docs.get('content').forEach(function(doc){
                var metadataPromise = doc.get('metadata');
                metadataPromise.then(function(metadata){
                    metadata.get('content').forEach(function(metadatum){
                        if (metadatum.get('type') == "collection" && metadatum.get('slug') == slug) {
                            results.push(doc);
                            self.set('filteredResults',results);
                        }
                    });
                });
            });
        });
        return ""
}.property('controllers.research_search.content','content','controllers.application.currentPath','controllers.application.currentRouteName'),

filteredResults: [],

@thomasqbrady
Copy link
Author

Json for docs:

{
"docs": [
{
"__v": 1,
"_id": "5295802d8789d43f7a00000c",
"createdAt": "2013-11-27T05:16:29.098Z",
"file_name": "01042010_bdo",
"fiscal_year": "2010",
"id": "5295802d8789d43f7a00000c",
"img": "",
"metadata": [
"5295802d8789d43f7a00037d",
"5295802d8789d43f7a000392",
"5295802d8789d43f7a000361",
"5295802d8789d43f7a00038b",
"5295802d8789d43f7a00036a",
"5295802d8789d43f7a000386",
"5295802d8789d43f7a000384",
"5295802d8789d43f7a000378",
"5295802d8789d43f7a000360"
],
"name": "OOO January 04, 2010",
"pub_date": "2010-01-04T06:00:00.000Z",
"short_desc": "In Consectetuer Ipsum Nunc Id Enim. Curabitur Massa. Vestibulum Accumsan Neque Et Nunc. Quisque Ornare Tortor At Risus.",
"type": "ooo"
}
],
"metadata": [
{
"__v": 0,
"_id": "5295802d8789d43f7a00035b",
"id": "5295802d8789d43f7a00035b",
"type": "author",
"value": "Thomas Brady"
}
]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment