Sencha touch 2 error, identifier generation strategy for the model does not ensure unique id's, while working with a store

StackOverflow https://stackoverflow.com/questions/23129918

  •  05-07-2023
  •  | 
  •  

Question

In Sencha touch, I have defined a store:

Ext.define('TestApp.store.TokenStore', {
    extend: 'Ext.data.Store',    
    config: {
        model: 'TestApp.model.TokenModel',
        autoLoad: true,
        autoSync: true,
        proxy: {
            type: 'localstorage',
            // The store's ID enables us to eference the store by the following ID.
            // The unique ID of the Store will be the ID field of the Model.
            id: 'TokenStore'
        }
    }
});

And a model for this store:

Ext.define('TestApp.model.TokenModel', {
    extend: 'Ext.data.Model',
    config:{
        fields: [ 
            {name: 'id', type: 'int' },
            {name:'token',type:'string'}
        ]
    }
});

Now, inside the launch function of my App, I do the following:

// Get the store
var tokenStore = Ext.getStore('TokenStore');

// Add a token to the store
tokenStore.add({'token': 1234});

// Retrieve that token back:
var token =  tokenStore.getAt(0).get('token');

Everything works, I see the token's value in the console, but I get the following warning:

[WARN][Ext.data.Batch#runOperation] Your identifier generation strategy for the model does not ensure unique id's. Please use the UUID strategy, or implement your own identifier strategy with the flag isUnique.

What am I doing wrong?

Was it helpful?

Solution

Add this inside your config on the model:

identifier: {
        type: 'uuid'
    },

Sencha Touch requires each record across all classes to have a identifier. Basically, that's a class that assigns a string to each record. There's javascript classes in the touch source that generate these. Those classes must declare themselves either unique or not. uuid is the best to use that's included in sencha touch and declares itself as unique (with good reason if you take a look at the math it does based on timestamps!)

The reason you need unique identifiers is so that records are not mixed up with each other, especially when it comes to DOM interactions or saving/loading them via a proxy.

OTHER TIPS

This will get rid of the warning and subsequent problems loading/saving, which will otherwise crop up after a number of saves/retrieves on the same store (the store will get eventually corrupted)

    identifier: {
        type: 'uuid',
        isUnique : true
    },

Tested in Chrome

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top