Question

I am working in extjs4 MVC.I am going to stuck at a point where I am going to set dynamically proxy type to 'localstorage' where I am replacing my proxy type which is 'ajax' which is declared in model class. When I want to store data at client side for that I am changing my model proxy type from ajax to locastorage.but when I am calling save() method on particular model object that data are going to server side not saved at client side.Plese give me some suggestion

1) Here is my model data class

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) here is my some controller file code

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

        });

please give me some suggestion.....

Was it helpful?

Solution

I created a sample code for a better understanding of switching proxxies.

//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

OTHER TIPS

You can set the proxy using the method setProxy(), defined on the Ext.data.Model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top