Javascript 从动作中设置模型并在ember js中重新加载模板
我已从另一个动作控制器重定向到一个控制器。Javascript 从动作中设置模型并在ember js中重新加载模板,javascript,jquery,ember.js,jsonp,Javascript,Jquery,Ember.js,Jsonp,我已从另一个动作控制器重定向到一个控制器。 this.get('controllers.searchResult').send('searchDoc',query) 这里我使用AJAX请求获取数组对象 App.SearchResultController = Ember.ArrayController.extend({ serverURL: 'someURL', actions: { searchDoc: function(query) { $.ajax({
this.get('controllers.searchResult').send('searchDoc',query)代码>
这里我使用AJAX请求获取数组对象
App.SearchResultController = Ember.ArrayController.extend({
serverURL: 'someURL',
actions: {
searchDoc: function(query) {
$.ajax({
type: "GET",
url: serverURL + request,
data : 'q=' + query,
dataType : "JSON",
context : this, // to increase the scope of
statusCode : {
200 : function(response) {
var docs = [];
response.docs.forEach(function(doc) {
docs.push(App.Doc.create(doc));
});
// do something here so that
// docs get save in the model
// and result page get reload
},
400 : function() {
console.log('status 400 something went wrong');
}
}
});
}
}
});
我是新的余烬JS。我愿意在模型中存储/保存/添加此文档
对象,并重新加载我的路线搜索结果
您应该保留对控制器的引用,并在返回结果时使用它设置内容
例如:
App.SearchResultController = Ember.ArrayController.extend({
serverURL: 'someURL',
actions: {
searchDoc: function(query) {
var self = this; // keep a reference to the controller
$.ajax({
type: "GET",
url: serverURL + request,
data : 'q=' + query,
dataType : "JSON",
statusCode : {
200 : function(response) {
var docs = Ember.A();
response.docs.forEach(function(doc) {
docs.pushObject(App.Doc.create(doc));
});
self.set('content', docs); // use the controller reference to set the content
},
400 : function() {
console.log('status 400 something went wrong');
}
}
});
}
}
});
我还在示例中添加了余烬数组的用法。
设置内容
会触发视图更新
您可以使用以下命令转换到searchResult:
this.get('controllers.searchResult').send('searchDoc', query);
this.transitionToRoute('searchResult');
谢谢你的回答。我已经在使用它了,但是没有使用self
。尽管如此,我还是必须使用self.set('content',response.docs)代码>代替上述命令使其工作。