1. #1
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default Answered: Two separated instances of the Model with the same id are the same object. Why?

    Answered: Two separated instances of the Model with the same id are the same object. Why?


    Suppose we have this model:
    Code:
    Ext.define('MyApp.model.Module', {
            extend: 'Ext.data.Model',
      config: {
         fields: [
            {name: 'id', type: 'int'},
            {name: 'name', type: 'string'},
         ]
      }
    });
    Then I define these instances:
    Code:
    var entry_a = Ext.create('MyApp.model.Module', {id: 5, name:'fantastic'});
    var entry_b = Ext.create('MyApp.model.Module', {id: 5, name:'awful'});
    entry_a.getData() outputs:

    Object
    id: 5
    name: "awful"


    entry_b.getData() outputs:

    Object
    id: 5
    name: "awful"

    As you can see the entry_a and entry_b seem to be the same object. I would expect these two entries to be two different objects. I had some nasty bugs because of this. So my questions would be:

    Why does it work this way? It is not intuitive at all. Am I misunderstanding something?

    Should I generate the id randomly and rename the current id to something like server_entry_id to have two separate objects?
    Last edited by arael78; 12 Oct 2012 at 1:22 AM. Reason: spelling

  2. This code works.

    Code:
     Ext.define('MoodleMobApp.model.Module', {
       extend: 'Ext.data.Model',
        
        config: {
            idProperty: 'localid',
            identifier: 'uuid',
    
    
            fields: [
                {name: 'localid', type: 'auto'},
                {name: 'id', type: 'int'},
                {name: 'courseid', type: 'int'},
                {name: 'instanceid', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'intro', type: 'string'},
                {name: 'modname', type: 'string'},
                {name: 'type', type: 'string'},
                {name: 'timemodified', type: 'int'},
                {name: 'isnew', type: 'boolean'},
                {name: 'isupdated', type: 'boolean'},
            ]
        }
    });
    Searching and modifying entries is not as easy as before because I need to use the queryBy() or findExact() function whereas before the getById was at my disposal.

    This fixes the problem of the silent updates though.

    I think that this code is just a poor fix to what I consider broken design. But that's only my opinion and I will be happy to change my mind if any developer has a good argument that justifies the current design.

  3. #2
    Sencha Premium Member intellix's Avatar
    Join Date
    Mar 2012
    Location
    UK + Malta
    Posts
    263
    Answers
    5
    Vote Rating
    18
    intellix will become famous soon enough

      0  

    Default


    And you think that's the end of it. You know what is even worse?

    If you have two separate Model definitions and they use the same ID then they're also regarded as the same thing when cache is used which is enabled by default and required for getBelongsToThing().

    Code:
    Ext.define('App.model.Person', {
        extend: 'Ext.data.Model',
    
    
        config: {
            fields: [
                {name: 'id', type: 'int'},
                {name: 'name', type: 'string'},
            ]
        }
    });
    
    
    Ext.define('App.model.Cup', {
        extend: 'Ext.data.Model',
    
    
        config: {
            fields: [
                {name: 'id', type: 'int'},
                {name: 'name', type: 'string'},
            ]
        }
    });
    
    
    var person = Ext.create('App.model.Person', {id: 5, name:'Dominic'});
    var cup = Ext.create('App.model.Cup', {id: 5, name:'Yellow one'});
    Not quite sure how that was an afterthought... every single one of my tables in my DB are set to autonumber primary key. Why would I want them to be regarded as the same thing?

    When you've wasted a couple of days/weeks following the tutorials and examples... it would have been nice to have read a note "PS - Models do not work at all yet". There's always at least one quirk per release that kicks you in the nuts with using models.

    ST 2.0.0 - Cache doesn't work and all is good
    ST 2.0.1 - Cache is fixed and my site breaks (so I remove it from source)
    ST 2.1.0b3 - Cache is required for getting hasBelongsToInstances (so I enable it across everywhere). removeAll() stops removing hasMany instances inside.

  4. #3
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    Quote Originally Posted by intellix View Post
    And you think that's the end of it. You know what is even worse?

    If you have two separate Model definitions and they use the same ID then they're also regarded as the same thing when cache is used which is enabled by default and required for getBelongsToThing().
    0___o Did I understand well? Two different instances of different Models with the same id are the same object?

    So how did you solve this issue? By using the UUID everywhere?

  5. #4
    Sencha Premium Member intellix's Avatar
    Join Date
    Mar 2012
    Location
    UK + Malta
    Posts
    263
    Answers
    5
    Vote Rating
    18
    intellix will become famous soon enough

      0  

    Default


    You did understand that
    Sorry I didn't post any sort of solution.

    I just prefix my IDs as you said with stuff like: person-5, cup-5

  6. #5
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    Quote Originally Posted by intellix View Post

    I just prefix my IDs as you said with stuff like: person-5, cup-5
    The framework does this on its own in the local storage. So you do that to avoid cache problems right?

    I don't want to change my server response object because of the cache problems in the sencha framework. There must be a better way. :/ At least I hope.

    My problem is when I have some records from the local storage cached and I load some updates from the server. The cached records are automatically updated without even letting me check what has changed.

    This shouldn't happen and it is a huge issue in my case because I would like to check and notify the user what has been updated. Silent updates are not appreciated.

    Are there any sencha developer that can give us a suggestion or an explanation on how to avoid/fix this issue?

  7. #6
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64
    Vote Rating
    20
    haduki will become famous soon enough haduki will become famous soon enough

      0  
    I write English by translator.

  8. #7
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    Your solution implies that I need to change the field id to something else.


    The problems is that the model is used by a store which uses an AJAX proxy. The url in the proxy is a moodle web service and the id field is the standard field in the responses of my web services.

    I cannot change the id field to something else because this would mean change the response in every web service I use.

  9. #8
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    Code:
    {
    allowNull: true,                 
    name: 'entityId',                 
    type: 'int'                 
    /*if you can not change your ajax response,
    add this code(in the quotes): ",mapping:'id'" */
    },


    Sorry, I totally overlooked this part in your post. I'll try this.

  10. #9
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    I have tried and it doesn't work.

    Here is my code, the problem is still there.

    Code:
    Ext.define('MoodleMobApp.model.Module', {
        extend: 'Ext.data.Model',
        
        config: {
    
            fields: [
                {name: 'entityId', type: 'int', mapping: 'id'},
                {name: 'courseid', type: 'int'},
                {name: 'instanceid', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'intro', type: 'string'},
                {name: 'modname', type: 'string'},
                {name: 'type', type: 'string'},
                {name: 'timemodified', type: 'int'},
                {name: 'isnew', type: 'boolean'},
                {name: 'isupdated', type: 'boolean'},
            ]
        }
    });

  11. #10
    Sencha User
    Join Date
    Aug 2012
    Posts
    51
    Answers
    2
    Vote Rating
    1
    arael78 is on a distinguished road

      0  

    Default


    This code works.

    Code:
     Ext.define('MoodleMobApp.model.Module', {
       extend: 'Ext.data.Model',
        
        config: {
            idProperty: 'localid',
            identifier: 'uuid',
    
    
            fields: [
                {name: 'localid', type: 'auto'},
                {name: 'id', type: 'int'},
                {name: 'courseid', type: 'int'},
                {name: 'instanceid', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'intro', type: 'string'},
                {name: 'modname', type: 'string'},
                {name: 'type', type: 'string'},
                {name: 'timemodified', type: 'int'},
                {name: 'isnew', type: 'boolean'},
                {name: 'isupdated', type: 'boolean'},
            ]
        }
    });
    Searching and modifying entries is not as easy as before because I need to use the queryBy() or findExact() function whereas before the getById was at my disposal.

    This fixes the problem of the silent updates though.

    I think that this code is just a poor fix to what I consider broken design. But that's only my opinion and I will be happy to change my mind if any developer has a good argument that justifies the current design.

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