PDA

View Full Version : model associations



toxkillfraex
10 May 2012, 1:13 AM
I have a model with a single association and it's possible for the user to edit the whole association.

I'm just terribly confused with how to edit it and how to create a new instance with an association nested in it

//ye model


Ext.define('EPA.model.DynamicFolder',
{
extend: 'Ext.data.Model',
requires: ['EPA.model.Filter'],

fields:
[
{name: 'id', type: 'int', defaultValue: null, useNull: true},
{name: 'label', type: 'string'},
{name: 'filter', type: 'string'}
],
associations:
[
{type: 'hasMany', model: 'EPA.model.Filter', name: 'filter', associationKey: 'filterParameters'}
]
});


the associated filter model


Ext.define('EPA.model.Filter',
{
extend: 'Ext.data.Model',

fields:
[
{name : 'parameter', type: 'string'},
{name : 'invert', type: 'boolean'},
{name : 'mode', type: 'int'},
{name : 'input', type: 'string'}
]
});


now when I get to the point of editing or creating a new one I have all the data needed to fill these variables. The new or edited filter(s) come in on an array.

when I create a new one I try this


var dynamicFolder = Ext.create('EPA.model.DynamicFolder',
{
label: label,
filter: json.content.filter,
filterStore: filters
});

in there filters is the array of new/edited filter models

but obviously that doesn't create the expected effect and neither did naming it filters instead of filterStore.

with edit I tried this


oldFolder.set('label', label);
oldFolder.set('filter', json.content.filter);
oldFolder.set('filters', filters);


neither of those obviously work so how can I create a new instance with an associated set of filters
and how do I replace the old ones when I edit an existing instance?

toxkillfraex
10 May 2012, 11:29 PM
I fixed the problem I had by creating a new store with the new data and using the association model and setting it after adding a new instance or while editing an old one