Wait! Looks like we don't have enough information to add this to bug database. Please follow this template bug format.
  1. #1
    Sencha User aflx's Avatar
    Join Date
    Jun 2011
    Location
    Berlin, Germany
    Posts
    64
    Vote Rating
    0
    aflx is on a distinguished road

      0  

    Default Phantom is set to false while adding new record to store

    Phantom is set to false while adding new record to store


    When I'm adding a new record to a store the phantom property is always set to false. So the new record will not be saved because you can find the following code in Ext.data.Store.insert():

    Code:
    ...
    sync = sync || (record.phantom === true);
    ...
    if (me.getAutoSync() && sync) {
        me.sync();
    }
    ...
    So you have to set phantom to true by hand, after creating a new instance of a model and before adding it.
    I think this should not be the default behavior!?

    Bye,
    Alex

  2. #2
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    10
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    Are you giving the record that you are adding an id? The only way a record becomes flagged as non-phantom when creating is when you pass in an id into the data.

  3. #3
    Sencha User aflx's Avatar
    Join Date
    Jun 2011
    Location
    Berlin, Germany
    Posts
    64
    Vote Rating
    0
    aflx is on a distinguished road

      0  

    Default


    No, I'm not giving an id to the new object by hand. I wanted to use the idgen to handle this (which doesn't work because of this bug: http://www.sencha.com/forum/showthread.php?177278-idgen-not-working-in-PR4).
    The id is set by the framework to something like this: "ext-record-23", when I create a new instance of a model like
    Code:
    var newObject = new MyModel()

    I have to add that my code worked well with PR3.

  4. #4
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    10
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    And if you do console.log(newObject.phantom) on the next line that shows a false value?

  5. #5
    Sencha User aflx's Avatar
    Join Date
    Jun 2011
    Location
    Berlin, Germany
    Posts
    64
    Vote Rating
    0
    aflx is on a distinguished road

      0  

    Default


    Yep.

  6. #6
    Sencha User
    Join Date
    Sep 2011
    Posts
    19
    Vote Rating
    2
    marcuswu is on a distinguished road

      0  

    Default


    I am experiencing this same issue. I'm not trying to add to a specific store, but saving it via the model's configured proxy which is attempting an update instead of a create/insert due to the phantom flag being false.

    The code looks like this:
    Code:
            contact = Ext.create('MyApp.model.Contact', {
                source: 'local',
                name: contactData.name.formatted,
                first_name: contactData.name.givenName,
                last_name: contactData.name.familyName
            });
            console.log('contact phantom value: '+contact.phantom);
            contact.save({
                callback: function(records) {
                    self.saveContactMeta(records[0], contactData);
                }
            });
    The phantom value outputs as false and the save ends up passing the proxy an update operation which fails for me because the backend I'm using doesn't have the record. I can work around it by setting phantom manually, but it shouldn't be necessary.

  7. #7
    Sencha User aflx's Avatar
    Join Date
    Jun 2011
    Location
    Berlin, Germany
    Posts
    64
    Vote Rating
    0
    aflx is on a distinguished road

      0  

    Default


    In PR3 there was this in the statics section:

    Code:
    /**
     * Generates a sequential id. This method is typically called when a record is {@link Ext#create
     * create}d and {@link #constructor no id has been specified}. The id will automatically be assigned to the
     * record. The returned id takes the form: {PREFIX}-{AUTO_ID}.
     *
     * - **PREFIX** : String - Ext.data.Model.PREFIX (defaults to 'ext-record')
     * - **AUTO_ID** : String - Ext.data.Model.AUTO_ID (defaults to 1 initially)
     *
     * @param {Ext.data.Model} rec The record being created. The record does not exist, it's a {@link #phantom}.
     * @return {String} auto-generated string id, `"ext-record-i++"`;
     * @static
     */
    id: function(rec) {
        var id = [this.PREFIX, '-', this.AUTO_ID++].join('');
        rec.phantom = true;
        rec.internalId = id;
        return id;
    }
    There the phantom was set to true, when a record is created.

    Alex

  8. #8
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    10
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    The reason we don't need that is because the logic in the constructor of Model sets phantom to true unless the Model gets a specific id in it's data. Is there any way you guys can give me some more code samples that allow me to replicate the issue? I have tried everything and am not able to create a model that is not phantom if I don't pass in an id to the constructor or in the data.

    Code:
            id = me.data[idProperty];
            if (!id && id !== 0) {
                me.data[idProperty] = me.internalId = me.id = me.getIdentifier().generate(me);
                me.phantom = true;

  9. #9
    Touch Premium Member
    Join Date
    Aug 2011
    Posts
    34
    Vote Rating
    0
    Pandorian is on a distinguished road

      0  

    Default


    I get the same problem too. I would place the caveat that I am new to Sencha Touch, so it could easily be my knowledge that is the problem.

    Model:
    Code:
    Ext.define('XentorMobile.model.UText', {
        extend: 'Ext.data.Model',
    
    
        config: {
    
    
            clientId: 'id',
    
    
            fields: [
                { name: 'id', type: 'int' },
                { name: 'astring', type: 'string'}
            ]
        }
    
    
     });
    Code:
    Ext.define('XentorMobile.store.UText', {
        extend: 'Ext.data.Store',
        requires: 'XentorMobile.model.UText',
    
    
        config: {
            model: 'XentorMobile.model.UText',
    
    
            proxy: {
                type: 'ajax',
    
    
                api: {
                    update: 'Home/UpdateUText',
                    create: 'Home/AddUText',
                    read:     'Home/GetUText',
                },
    
    
    /*
                  headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
    */
    
    
                actionMethods: {
                    read: 'POST',
                }
            },
        
            autoLoad: true
        }
    
    
    });
    Code:
            var store = this.getUTextStore();
    
    
             var rec = Ext.create('XentorMobile.model.UText', {astring:'Hello'}) 
    
    
            console.log(rec.phantom);
    
    
              rec.phantom=true;
             rec.save();
    Also, it does not appear to be sending any data back to the server?
    Attached Images

  10. #10
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    10
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    Alright, thanks for the examples again. I'm gonna try and dive into this one again. I just realized this might have something to do with a defaultValue being set on an id field. I'm gonna investigate and get back to you guys.