How to find model with Ember Data?

后端 未结 2 1937
星月不相逢
星月不相逢 2021-01-27 16:24

What\'s the equivalent of

App.Person.find({age: 30})

in the new Ember Data?

I.e. how do I get an record array based on an attribute?

相关标签:
2条回答
  • 2021-01-27 16:48

    There are actually a couple ways of doing this. In this example, the model is "Org", the parameter is all org names with name "AAA":

    this.store.find('org', {'name' : 'AAA'})
    

    Or

    App.Org.store.find('org', {'name' : 'AAA'})
    

    They would both return the same Ember Promise Array.



    To work with individual elements in the array you can do:

    this.store.find('org', {'name' : 'AAA'}).then( function(result) {
         // do stuff with each result
    });
    

    Similarly,

    App.Org.store.find('org', {'name' : 'AAA'}).then( function(result) {
         // do stuff with each result
    });
    


    Everything here and more is shown in this jsbin, so you can compare and play yourself. Don't forget to see the results by opening the console.

    Thanks to Mike's comment.

    0 讨论(0)
  • 2021-01-27 16:59

    The equivalent method in ember data 1.0.0 beta2 is now:

    this.store.find('person', {age: 30})
    

    More info on all the changes can be found here.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题