Hybrid View

  1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    224
    Vote Rating
    9
    dedoz will become famous soon enough

      0  

    Default Working with associated models #5 - hasOne setter problem

    Working with associated models #5 - hasOne setter problem


    from the previus post
    http://www.sencha.com/forum/showthre...ocs)-problem-2

    this applies to Extjs 4.1 beta 1 beta 2 and beta 3 source code.
    (having the correct concept of has one) “Has one” setter should take the given id and set the associated model id with this value, since the forerign key is in the associated model, not as “belongs to” where the foreign is in the owner model.
    Heres a test case to check the errors.

    these models represent person (per model ) and user account (usr model)
    associations are "person has one user account" and "user account belongs to a person"
    Code:
        
        Ext.define('per', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'perId'    ,type:'int'}
            ],
            hasOne:[
                {model:'usr',    associationKey:'usr',    primaryKey:'perId',    foreignKey:'perId',     getterName:'usr',    setterName:'usrSet'}
            ],
            idProperty: 'perId'
        });
        
        Ext.define('usr', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'usrId'    ,type:'int'},
                {name:'perId'    ,type:'int'},
            ],
            belongsTo:[
                {model:'per',    associationKey:'per',    primaryKey:'perId',    foreignKey:'perId',    getterName:'per',    setterName:'perSet'}
            ],
            idProperty: 'usrId'
        });
    first testing belongsTo association (belongsTo source code is fine) just to check if i models are working as expected
    Code:
      
       // Testing belongsTo id propagation
        var per = Ext.create('per',{perId : 4});
        var usr = Ext.create('usr',{usrId : 7});
        usr.perSet(per); // appending per to usr should set usr.perId to 4
        console.log('BelongsTo test');
        console.log(per.data); // nothing has changed, which is correct.
        console.log(usr.data); // usr.perId now its 7, which is correct.
    Evething went well with belongsTo, so models are well definided. Now checking hasOne, wich i know it has errors.
    Code:
        
        // Testing hasOne id propagation    
        var per = Ext.create('per',{perId : 4});
        var usr = Ext.create('usr',{usrId : 7});
        per.usrSet(usr); // appending usr to per should set usr.perId to 4
        console.log('HasOne test');
        console.log(per.data); // per.perId now its 7, copied the usr.usrId to per.perId as an expected error.
        console.log(usr.data); // nothing has changed, usr.perId wasnt set
    the source code with problems and the fix.
    this code is from the source of HasOne association (red comments are mine)
    Code:
    createSetter: function() {
            var me              = this,
                ownerModel      = me.ownerModel, // unused var ?
                foreignKey      = me.foreignKey;
            //'this' refers to the Model instance inside this function
            return function(value, options, scope) {
                // wrong code start -----------------------------------------------------------
                if (value && value.isModel) { //value shouldnt optionally be a model, always must be a model.
                    value = value.getId(); 
                }
                this.set(foreignKey, value); // shouldnt set a value to "this" model
                                             //  the id should propagate from "this" model
                                             //  to the associated model (passed as first parameter "value")
                                             // thats the conceptual meaning of "hasOne"
                // wrong code end -----------------------------------------------------------
                if (Ext.isFunction(options)) {options = {callback: options,scope: scope || this};}
                if (Ext.isObject(options)) {return this.save(options);}
              };
        },
    the fix
    Code:
        createSetter: function() {
            var me              = this,
                ownerModel      = me.ownerModel,
                foreignKey      = me.foreignKey;
            //'this' refers to the Model instance inside this function
            return function(value, options, scope) {
                
                value.set(foreignKey,this.getId()); // value must be always a model
                                                    // this line should replace the wrong code
                
                if (Ext.isFunction(options)) { options = {callback: options,scope: scope || this};}
               if (Ext.isObject(options)) { return this.save(options);}
           };
        },

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,640
    Vote Rating
    898
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Bugs forum...
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Dec 2011
    Posts
    224
    Vote Rating
    9
    dedoz will become famous soon enough

      1  

    Default


    mitchel theres a reason to post in discussion forums and not in Q&A or Bugs, the reason is im new to extjs im not sure this problems are bugs, so i post here just to get some feeback as you did in one saying "thats not 100% correct". after i im sure thats a bug (maybe) ill post in bugs forum. I dont expect ppl posting "post in another forum" thats not even a comment, over that you are forum manager i guess u can move posts but bug forums has a format. after hours trying to figure the problem theres no "extra" hours left to post using the bugs forum format, i have my own work and i dont work for sencha, i think i did my part sharing my notes so other ppl dont waste their time, if its a real bug.

Thread Participants: 1