0

I'm using ExtJS 4.0.7 with the ExtJS MVC features. I have a parent model which hasMany children which belongsTo the parent.

What is the correct way to access the children? If I go through parent.children().data.items[0].data (as opposed to parent.data.children[0]), there is an unwanted property MyApp.model.parent_id. I've also noticed differences between how the dates are stored.

First, here is the definition of the parent. So a parent will have many children.

Ext.define('MyApp.model.Parent', {
    extend : 'Ext.data.Model',
    fields : [ 'id' ],
    hasMany : [ {
        model : 'MyApp.model.Child',
        name : 'children'
    }],
    proxy : {
        type : 'direct',
        api : {
            create : Parents.create,
            read : Parents.read,
            update : Parents.update,
            destroy : Parents.destroy
        }
    }
});

And each child will belong to a parent.

Ext.define('MyApp.model.Child', {
    extend : 'Ext.data.Model',
    fields : [ 'id', {
        name : 'validFrom',
        type : 'date',
        dateFormat : 'time'
        } ],
    belongsTo : 'Parent'
});

I load a parent in my controller:

this.getParentModel().load(id, {
    scope : this,
    success : function(parent) {
                    // the party looks good here when logged to console
                    console.log(parent);
        this.updateStore(parent);
    }
});

When inspecting the parent in the console, this is what I find:

  1. console.log(parent.data.children[0]):

Console output:

Object
id: 3
validFrom: -1767225600000
__proto__: Object
  1. console.log(parent.children().data.items[0].data):

Console output:

Object
id: 3
MyApp.model.parent_id: 209 // why is that here?
validFrom: Thu Jan 01 1914 01:00:00 GMT+0100 (Central European Standard Time)
__proto__: Object

enter image description here

ipavlic
  • 4,906
  • 10
  • 40
  • 77
  • Although I'm sure your question is clear to you, it's hard to understand/answer with that little code shown. Could you please share the definition of both models? – Izhaki Oct 11 '12 at 15:23
  • @Izhaki I'm sorry about the lacking information. I have edited my question heavily, and hopefully made it clearer (I cleared up some things for me as well). – ipavlic Oct 12 '12 at 08:19

1 Answers1

0

If I understand correctly, I think you want:

var someRecordId = 'idProperty';
var record = Ext.getStore('Parent').getById(someRecordId);

// this returns all hasMany for the name of of the property i.e. name: 'child'

record.child();  

This will return you all of the items data (based on the model definition) in Child for the Parent record. It should only include those fields in the Child model definition. So parent_id will not be included. If you're trying to retrieve all records for Child, then you just get them the same way you would any other store.

radtad
  • 189
  • 5