I have a problem with the stores. Whenever I update an entry it the id are duplicated.
Here is my model.
Code:
Ext.define('MoodleMobApp.model.Course', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'auto'},
{name: 'name', type: 'string'},
{name: 'timemodified', type: 'int'},
{name: 'token', type: 'string'},
{name: 'modules', type: 'int', defaultValue: 0},
{name: 'isnew', type: 'boolean'},
{name: 'newmodules', type: 'int', defaultValue: 0},
{name: 'modulestatus', type: 'string', defaultValue: 'counting modules...'},
]
}
});
Here is the store:
Code:
Ext.define('MoodleMobApp.store.Courses', {
extend: 'Ext.data.Store',
requires: [
'MoodleMobApp.model.Course',
'Ext.data.proxy.LocalStorage'
],
config: {
storeId: 'courses',
model: 'MoodleMobApp.model.Course',
autoLoad: true,
proxy : {
id: 'course',
type: 'localstorage',
}
}
});
Here is the code that adds/updates new entries:
Code:
// update local courses store
this.each(
function(entry) {
if(courses_store.find('id', entry.getData().id) == -1) {
entry.getData().isnew = true;
courses_store.add( entry );
} else {
entry.getData().isnew = false;
// update the entry
courses_store.remove( entry );
courses_store.add( entry );
}
}
);
// prepare to write
courses_store.each(
function() {
this.setDirty();
}
);
// store data
courses_store.sync();
When I execute this code the number of the records is the same but the ids are duplicated.
So when I have three records courses_store.getCount() === 3 but if I check in the localstorage the course key contains this: 3,5,8,3,5,8.
If I execute this code one more time I get: 3,5,8,3,5,8,3,5,8.
Everything works but the course value keeps growing.
How can I fix this issue?
Thank you very much for any suggestions.