简体   繁体   中英

How to use nested call to DataStore in emberJS?

I am trying to have a call the datastore from within THEN() function again to retrieve data from second DS based on founding of FIRST datastore, but "this" is not available in THEN() function. How to do something similar to following example. The second call to this.store.findRecord does not work?

    actions:{

    onBookStoreSelect(bookstore){

            console.log(bookstore.id);
            this.store.findRecord('boston_city_bookstores', bookstore.id).then(function(response){
                console.log(response);
                console.log(response.get('bookstore_address'));
                console.log(response.get('bookstore_owner'));
                console.log(response.get('bookstore_key_ID'));

                this.store.findRecord('books', response.get('bookstore_key_ID')).then(function(bookList){

                    //do something
            });

You can use arrow function to maintain this reference.

onBookStoreSelect(bookstore) {        
        return this.store.findRecord('boston_city_bookstores', bookstore.id).then((response) => {            
            return this.store.findRecord('books', response.get('bookstore_key_ID')).then((bookList) => {
                //do something and return the result if you are using it in caller place.
            });
        });
    }

Refer:
http://es6-features.org/#Lexicalthis https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM