Jquery 坚持使用getJson.then()函数和this.store.find
我有以下应用程序控制器:Jquery 坚持使用getJson.then()函数和this.store.find,jquery,ember.js,Jquery,Ember.js,我有以下应用程序控制器: App.ApplicationController = Ember.Controller.extend({ // the initial value of the `search` property query: '', actions: { searchQuery: function () { // the current value of the text field var
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));
}
});
}
}
}
});
我被第二个呼叫卡住了。TransitionRoute
呼叫。它在同一个控制器中。但是,最后一个返回未捕获类型错误:无法调用未定义的方法“find”
这与getJSON方法有关吗?我尝试在getJSON调用之外调用转换。但这样的承诺就行不通了
有人经历过这种调用吗?
此
更改然后
的内部范围,在then外部设置对此
的引用,并在then内部使用该引用(如我在下面使用self
时所做的)
此
更改然后
内部的范围,在然后外部设置对此
的引用,并在然后内部使用该引用(如我在下面使用self
)
非常感谢。当我编辑我的问题以获取更多信息时,您已经键入了答案!然而,我把我的问题称为17点编程停电。我知道这种感觉,它发生在我们所有人身上。谢谢!当我编辑我的问题以获取更多信息时,您已经键入了答案!然而,我把我的问题称为17点编程停电。我知道这种感觉,它发生在我们所有人身上。
App.ApplicationController = Ember.Controller.extend({
// the initial value of the `search` property
query: '',
actions: {
searchQuery: function () {
var self = this;
// 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
self.transitionToRoute('article', self.store.find('article', response.id));
}
});
}
}
}
});