4

我在 extjs4 MVC 中工作。我将停留在我将动态代理类型设置为“localstorage”的地方,我将替换我的代理类型,即在模型类中声明的“ajax”。当我想在客户端存储数据时,我将模型代理类型从 ajax 更改为 locastorage。但是当我在特定模型对象上调用 save() 方法时,数据将发送到服务器端而不是保存在客户端。请给我一些建议

1)这是我的模型数据类

Ext.define('Am.model.sn.UserModel',{
    extend: 'Ext.data.Model',
    fields: ['userId','firstName','middleName','lastName','languageId','primaryEmail','birthDate','password','securityQuestionId','securityQuestionAnswer','isMale','creationTime','ipAddress','confirmationCode','userStatusId',],
    proxy:
    {
        type:'ajax',
        api:
        {
            read:'index.php/SocialNetworking/user/AuthenticateLogin',
            create:'index.php/SocialNetworking/user/AuthenticateLogin',
        },//end of api
        reader:
        {
            type:'json',
        },//end of reader
        writer:
        {
            type:'json',
            root:'records',
        },//End of writer
    }//end of proxy
});

2)这是我的一些控制器文件代码

Ext.define('Am.controller.sn.UserController',
        {

            extend:'Ext.app.Controller',

            stores:['sn.UserStore','sn.SecurityquestionStore'],
            models:['sn.UserModel','sn.SecurityquestionModel'],
            views:['sn.user.Login','sn.user.Registration','sn.user.ForgetMyKey','sn.user.SecurityQuestion','sn.user.KpLogin'],
            -----
            ----
            init:function()
            {
                --------
            }
            remeberMe:function()
{
    console.log("check box selected");
    var email=this.getUserName().getValue();
    var password=this.getPassword().getValue();
    var objCheckBox=Ext.getCmp('checkbox');
    if(objCheckBox.getValue()==true)
    {
        window.localStorage.clear();
        //code for stoaring data at local storage
        var modelObject = Ext.ModelManager.create(
        {
            primaryEmail:email,
            password: password,
        }, 'Balaee.model.sn.UserModel');
        proxy=modelObject.getProxy();
        //proxy=modelObject.getProxy();
        proxy.type='localstorage';
        //proxy.set(type,'localstorage');
        proxy.id='rememberMe';
        //proxy.set(id,'rememberMe');
        //modelObject.setProxy(proxy);
        //console.log("models proxyyyyyyyyyy="+modelObject.getProxy().type+""+modelObject.getProxy().id);

        modelObject.setProxy(proxy);
        //
        I am also trying this but not work
        //Ext.apply(proxy,{type:'localstorage',id:'remember' });

        modelObject.save();
            // code to hide login window
            //var obj=Ext.ComponentQuery.query('#loginId');
            //console.log("Object name = "+obj[0].id);
            //obj[0].hide();
    }//end of if statement
    else
    {
        console.log("check box is not selected");
    }//end of else statement
},//End of rememberMe  function

        });

请给我一些建议......

4

2 回答 2

4

我创建了一个示例代码,以便更好地理解切换代理。

//Defining model
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [{name: 'name',  type: 'string'}]
});
//creation of ajax proxy
var ajaxProxy = new Ext.data.proxy.Ajax({
    id: 'ajaxp',
    reader: 'json'
});
//creation of local storage proxy
var lsProxy = new Ext.data.proxy.LocalStorage({
    id: 'localp',
    reader: 'json'
});
//Create instance of model
var user = Ext.create('User', {
    name : 'Pravin Mane',
    proxy: ajaxProxy //sets the ajax proxy
});

//Somewhere in your code
user.setProxy(lsProxy); //sets the localstorage proxy
于 2013-03-05T14:00:51.457 回答
0

setProxy()您可以使用在上定义的方法设置代理Ext.data.Model

于 2013-03-05T09:23:10.930 回答