簡體   English   中英

如何在emberJS中使用對DataStore的嵌套調用?

[英]How to use nested call to DataStore in emberJS?

我試圖再次從THEN()函數中調用數據存儲區,以基於FIRST數據存儲區的建立從第二個DS檢索數據,但是THEN()函數中不提供“ this”。 如何執行類似於以下示例的操作。 第二次調用this.store.findRecord不起作用?

    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
            });

您可以使用箭頭功能維護this參考。

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.
            });
        });
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM