2011-11-11 47 views
1

我只是试图更新本地存储,但内部的Ext.Ajax.request我无法调用this.store.create()。如何在Ajax调用的success:区域内调用this.store.create函数。非常感谢您的帮助。this.store.create不会在ajax内呼叫

login: function(params) { 
    params.record.set(params.data); 
    var errors = params.record.validate(); 

    if (errors.isValid()) { 

     var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."}); 
     myMask.show(); 

     //now check if this login exists 
     Ext.Ajax.request({ 
      url: '../../ajax/login.php', 
      method: 'GET', 
      params: params.data, 
      form: 'loginForm', 
      success: function(response, opts) { 
       var obj = Ext.decode(response.responseText);  
       myMask.hide();  
       //success they exist show the page 
       if(obj.success == 1){ 
       //this doesn't work below 
            this.store.create(params.data); 
       this.index(); 
       }  
       else{ 
       Ext.Msg.alert('Incorrect Login'); 
       } 
      }, 
      failure: function(response, opts) { 
       alert('server-side failure with status code ' +  response.status); 
       myMask.hide(); 
      } 
     }); 
    } 
    else { 
     params.form.showErrors(errors); 
    } 
}, 
+0

什么是错误?你会得到一个JavaScript异常吗?这可能是一个范围问题,你的“这个”指针可能并不指向你的想法。此外,文档提到了商店的添加方法,但不是创建。您使用的是哪种版本的sencha touch? –

+0

即时通讯使用sencha 1.1我想我需要引用商店的完整商店名称?即:loginDetails.store.create(params.data);那是对的吗? –

回答

1

在Javascript中,“这个”的关键字改变其含义与它出现在。

当在对象的方法中使用,“这”是指该对象的方法立即属于上下文。在你的情况下,它指的是你传递给Ext.Ajax.request的参数。

要解决此问题,需要保留上层'this'的引用以便在内部上下文中访问其'store'属性。具体来说,它看起来像这样:

var me = this, 
    ....; 

Ext.Ajax.Request({ 
... 
success: function(response, opts) { 
       var obj = Ext.decode(response.responseText);  
       myMask.hide();  
       //success they exist show the page 
       if(obj.success == 1){ 
       me.store.create(params.data); 
       this.index(); 
       }  
       else{ 
       Ext.Msg.alert('Incorrect Login'); 
       } 
      }, 
}); 
+0

非常感谢。我需要弄清楚我是如何引用这个的。作为它的设置它是一个更复杂的时尚。有没有办法在任何时候参考您的商店?像:myStoreName.store.create(params.data)。 –

+0

我只需在商店的任何“顶级”方法中执行'var store = this'即可。 [Here](http://bonsaiden.github.com/JavaScript-Garden/#function.this)是阅读“this”关键字的好地方。 –

+0

我应该也只是使用数据存储而不是控制器内的ajax调用。 MVC –