Hybrid View

  1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    200
    Answers
    11
    Vote Rating
    4
    dedoz is on a distinguished road

      1  

    Default Unanswered: how to send associated model on record.save()

    Unanswered: how to send associated model on record.save()


    Hi.

    I have a Extjs model person and model phoneNumber (also respective model on database)
    " person has many phoneNumber "

    On ExtJs the model person has a association hasMany phoneNumber.

    When i create a person record i add the phoneNumbers to that record.
    Then i send to save the person record.
    Code:
    Ext.onReady(function() {
        Ext.direct.Manager.addProvider(Ext.app.REMOTING_API);
        
        Ext.define('Phone', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'phoId'        ,type:'int'},
                {name:'perId'        ,type:'int'},
                {name:'phoNumber'    ,type:'string'},
            ],
            idProperty: 'phoId',
            proxy: 
            {    type: 'direct',
                api:{
                    create    : phoQuery.insert,
                    read    : phoQuery.select,
                    update    : phoQuery.update,
                    destroy    : phoQuery.delete
                }
            }
        });
    
        Ext.define('Person', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'perId'    ,type:'int'},
                {name:'perName'    ,type:'string'},
            ],
            associations: [
                {     type: 'hasMany', model: 'Phone', name : 'phones', associationKey: 'phone' }
            ],
            idProperty: 'perId',
            proxy: 
            {    type: 'direct',
                api:{
                    
                    read    : perQuery.select,
                    create    : perQuery.insert,
                    update    : perQuery.update,
                    update    : perQuery.delete,
                    
                }
            }
        });
    
    
        Ext.define('PersonStore', {
            extend: 'Ext.data.Store',
            model: 'Person',
            autoLoad: false
        });
        
        // Logic
        var myPerson = Ext.create('Person');
        
        myPerson.set('perName','peter');
        
        myPerson.phones().add(
            {phoNumber : '12342425'},
            {phoNumber : '056-23-3232'}
        );
    
        myPerson.save();
        
    });
    this save() only sends the person data. How can i send the phone numbers too ? in the same call.

    In the server side i have a function (perInsert) to insert a new person to the dabase, and then, save his phones if theres any, using the person Id that the database asigned after the person insert. this is working.

    The problem is to send person + his phones to that server function.

    other ways im trying
    Send 2 saves (save and sync)
    Code:
      myPerson.save();
      myPerson.phonesStore.sync();
    cant do this because those phone numbers dont have the person id (perId) set yet so the server wont know to which person they belongs to. I cant set phones perId before the sync because the person doesnt have perId set neither, is set on the server and when that call is done ill know the perId that the server responded.

    Wait for person.save() to end
    i could wait (in some way i dont know) for the save() to return and then set the perId to all phones and do a sync() but that means " go to server to save person, back to client with perId info, go to server to save phones" too much calls when with raw ajax i know i can send all the data in 1 call and save all data .
    Anyway this is a solution at least, but i dont know how to wait for save() to return

    Use proxy.batch
    im reading the proxy.batch() doc, maybe with this i can send the Person model and the phone models in 1 call, but batch will send my Person to its proxy create method (perInsert) and then phones to (phoInsert). Thinking this still.

    have hours reading docs but cant find anything, im sad.
    any ideas on this ? how to send associated model on record.save() or "in one call to the server" ?
    (sorry for my bad english)

  2. #2
    Sencha User
    Join Date
    Aug 2012
    Posts
    11
    Vote Rating
    0
    kivras is on a distinguished road

      0  

    Default


    I have the exact same problem!

    I wonder why there is so little support for nested models, when it is a frequent problem.

    Also i can get the nested models store by calling
    Parent.{child_model_name}(); ----ex. store.phones();

    But how can i set those values???

    Thank you in advance!

  3. #3
    Sencha User existdissolve's Avatar
    Join Date
    Jan 2010
    Location
    Kansas
    Posts
    514
    Answers
    31
    Vote Rating
    82
    existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice

      0  

    Default


    If you need to wait for save() to return, you can easily do this:

    PHP Code:
    myPerson.save({
       
    success: function( recordsoperation ) {
          
    // response of save is returned...do other stuff
       
    }
    }); 
    The thing I don't like about this approach is that if the first item gets saved, but the second (in the callback) doesn't, your stuck with a pointless orphaned record.

  4. #4
    Sencha User
    Join Date
    Aug 2012
    Posts
    11
    Vote Rating
    0
    kivras is on a distinguished road

      0  

    Default


    No this isn't a solution.
    There will be many cases that i will need to send nested Json Objects on a call, for various reasons.

Thread Participants: 2

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar