坚持使用getJson .then()函数和this.store.find

时间:2013-11-19 16:41:38

标签: jquery ember.js

我有以下ApplicationController:

App.ApplicationController = Ember.Controller.extend({
    // the initial value of the `search` property
    query: '',

    actions: {
        searchQuery: function () {
            // the current value of the text field
            var query = this.get('search');

            // numeric search
            if (parseInt(query) > 0) {
                this.transitionToRoute('article', this.store.find('article', query)); // works
            }
            // string search
            else {
                // search database for the search keyword
                jQuery.getJSON("/search/article/" + query + "").then(function(response){
                    if(parseInt(response.id) > 0){
                        console.log(response.id); // 1 when searching a specific value

                    // Uncaught TypeError: Cannot call method 'find' of undefined     
                    this.transitionToRoute('article', this.store.find('article', response.id)); 
                    }
                });
            }

        }
    }
});

我被困在第二次this.transitionToRoute电话上。它在同一个控制器中。但是最后一个返回Uncaught TypeError: Cannot call method 'find' of undefined

是否与getJSON方法有关?我试图在getJSON调用之外调用转换。但那么承诺将无效。

有没有人遇到这种电话?

1 个答案:

答案 0 :(得分:1)

this更改then内部的范围,在then之外设置对this的引用,并在当时使用该引用(例如我在下面用{{1 }})

self