Sencha 2 : Sync models with hasMany associations in LocalStorage

Posted by Alytrem on Stack Overflow See other posts from Stack Overflow or by Alytrem
Published on 2012-03-19T14:37:19Z Indexed on 2012/11/26 5:04 UTC
Read the original article Hit count: 213

Filed under:
|

After hours and hours trying to do this, I need your help.

I have to models : Project and Task. A project hasMany tasks and a task belong to a project. Everyting works well if you don't use a store to save these models.

I want to save both tasks and projects in two stores (TaskStore and ProjectStore). These stores use a LocalStorage proxy.

I tried many things, and the most logical is :

Ext.define('MyApp.model.Task', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
        {
            name: 'name',
            type: 'string'
        },
        {
            dateFormat: 'd/m/Y g:i',
            name: 'start',
            type: 'date'
        },
        {
            dateFormat: 'd/m/Y g:i',
            name: 'end',
            type: 'date'
        },
        {
            name: 'status',
            type: 'string'
        }
        ],
        belongsTo: {
            model: 'MyApp.model.Project'
        }
    }
});


Ext.define('MyApp.model.Project', {
    extend: 'Ext.data.Model',
    alias: 'model.Project',
    config: {
        hasMany: {
            associationKey: 'tasks',
            model: 'MyApp.model.Task',
            autoLoad: true,
            foreignKey: 'project_id',
            name: 'tasks',
            store: {storeId: "TaskStore"}
        },
        fields: [
            {
                name: 'name',
                type: 'string'
            },
            {
                dateFormat: 'd/m/Y',
                name: 'start',
                type: 'date'
            },
            {
                dateFormat: 'd/m/Y',
                name: 'end',
                type: 'date'
            }
        ]
    }
});

This is my "main" :

var project = Ext.create("MyApp.model.Project", {name: "mojo", start: "17/03/2011", end: "17/03/2012", status: "termine"});
var task = Ext.create("MyApp.model.Task", {name: "todo", start: "17/03/2011 10:00", end: "17/03/2012 19:00", status: "termine"});
project.tasks().add(task);
Ext.getStore("ProjectStore").add(project);

The project is added to the store, but task is not. Why ?!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about sencha