Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to have an object.saveToLocalDB()

    Hi there. I'm trying to save js objects to the local db. I'd like to follow some kind of architectural pattern like Active Record, where each class has a Save method where it saves itself to the db. I can't make this work since in CategoryItem.save 'this' refers to the Window. I've tried other ways of declaring the function but I really can't make this work. Any help is appreciated!

    Code:
    function SaveWSDataToLocal(dataWS){
        if(dataWS!=null && dataWS.list!=null && dataWS.list.length>0){
            
            for(var a=0;a<dataWS.list.length;a++){
                //json to classes
                var categoryAux=new categoryItem(dataWS.list[a].category.id,dataWS.list[a].category.name);
                categoryAux.questions=ParsequestionsFromWS(dataWS.list[a].questions);
                //this is where i'd like to save the category, that has all the data
                db.transaction(categoryAux.save, errorScriptSql);
            }
        }else{
            alert("Error");
        }
    }
    
    function ParsequestionsFromWS(preguntas){
        var questionsArray=[];
        for(var i=0;i<questions.length;i++){
            var questionAux=new questionItem(questions[i].id, questions[i].question, preguntas[i].id_Option_correcta);
            //questionAux.options=ParseoptionsFromWS(questions[i].options);
            questionsArray.push(preguntaAux);
        }
        return questionsArray;
    }
    
    function CategoryItem(id, name){
        this.id=id;
        this.name=name;
        this.questions=[];
        this.save = function(transaction) {
            transaction.executeSql("INSERT INTO Categorys VALUES("+this.id+","+this.name+")");
            for(var i=0; i<this.questions.length;i++)
                this.questions[i].save(transaction, this.id);
        };
    }
    			
    function questionItem(id, question, id_Option_correcta, options){
        this.id=id;
        this.question=question;//text of the question
        this.id_Option_correcta=id_Option_correcta;
        this.options=[];
        this.save = function(transaction, idCategory) {
            transaction.executeSql("INSERT INTO questions VALUES("+this.id+","+this.question+","+this.id_Option_correcta+","+idCategory+")");
            for(var i=0; i<this.options.length;i++)
                this.options[i].save(transaction, this.id);
        };
    }
    			
    function OptionquestionItem(id, Option){
        this.id=id;
        this.Option=Option//texto Option
        this.save = function(transaction, idquestion) {
            transaction.executeSql("INSERT INTO options VALUES("+this.id+","+this.Option+","+idquestion+")");
        };
    }

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by RomiOrchids View Post
    I can't make this work since in CategoryItem.save 'this' refers to the Window.
    Code:
    db.transaction(categoryAux.save, errorScriptSql)
    can be

    Code:
    db.transaction(  categoryAux.save.bind(categoryAux) , errorScriptSql )
    so that this is always what you expect it to be inside the callback function.

    the other way to do it is to give a non-competing alias to this:

    Code:
    function CategoryItem(id, name){
       var that=this;
        this.id=id;
        this.name=name;
        this.questions=[];
        this.save = function(transaction) {
            transaction.executeSql("INSERT INTO Categorys VALUES("+that.id+","+that.name+")");
            for(var i=0; i<that.questions.length;i++)
                that.questions[i].save(transaction, that.id);
        };
    }
    (changes in red)
    Last edited by rnd me; 08-12-2014 at 03:33 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •