[英]Ember controller, promises, and possibly rerendering template in response to user search
我有一個Ember應用程序,是菜鳥。 最終用戶可以使用某種形式按特定日期搜索該特定項目。 控制器看起來像(並已在問題中發表評論):
App.IpbinshowController = Ember.ObjectController.extend({
actions:{
searchPeriod: function(params){
var inventory_item_id_val=this.get('id');
var start_date_val=this.get('start_date');
var end_date_val=this.get('end_date');
alert('this is what I want with start_date: ' + start_date_val + ' and end_date: ' + end_date_val + ' and inventory_item_id: ' + inventory_item_id_val);
var result = App.InventoryItem.find(inventory_item_id_val, {start_date: start_date_val, end_date: end_date_val }); // <- this works correctly
result.then(function(){ // This part doesn't work
this.set('model',result); // <- I get 'Uncaught TypeError: undefined is not a function' for this line
}); //
}
}
});
我怎樣才能解決這個問題? 並使其重新呈現。
謝謝
我覺得它會更接近於此:
var result = App.InventoryItem.find(inventory_item_id_val, {start_date: start_date_val, end_date: end_date_val });
result.then(function(){
console.log('here are result: ');
console.log(result);
result.done(function(r){
console.log('here are r: ');
console.log(r);
this.set('model', r);
}.bind(this));
});
因為console.log(r)輸出正確的信息,而我在下一行得到一個未定義的信息-那么在那時我該如何指代呢? 很抱歉提出一些簡單的問題-真的是我第一次處理這個問題。
其原因,用於接收此錯誤是this
是在不同的上下文中使用,並且不引用相同的對象,也指出了通過Hrishi
。 我通常遵循的一種簡單而通用的方法是將其分配給變量,然后改用它。
http://emberjs.jsbin.com/pokohuku/1/edit
JS
App.IndexController = Ember.ObjectController.extend({
testProp:"",
actions:{
searchPeriod: function(params){
var self = this;
var result= $.ajax({url:""});
result.then(function(){
self.set("testProp","testProp's value has been set!");
alert(self.get("testProp"));
});
}
}
});
你this
是錯的。 在this
在內部函數是不相同的this
在其定義的功能。 為了解決這個問題,您可以使用:
result.then(function(){
this.set('model',result);
}.bind(this));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.