Kerempuhh
Kerempuhh

Reputation: 103

How to get a value from controller to form

I've built a master/detail form in ExtJS4 as an exercise of learning ExtJS. I want to pass down id from master table to the detail form when i'm creating a new article. When the create button is pressed in detailGrid, the id is passed to the controller. In the controller I can see the output of the id via console.log so I know it is present in the controller. How can I pass it to the form?

Controller:

{    
    var detailgrid = button.up('userdetail');      
    var parentId = detailgrid.getParentId();
    console.log(parentId);
    var view = Ext.widget('detailadd');  
},

The Form where I need to get parentId from the controller

Ext.define('calsberg.view.user.DetailsAdd', {
    extend: 'Ext.window.Window',
    alias: 'widget.detailadd',
    title: 'Add Artikli',
    config:
    {
        parentId: parentId
    },
    layout: 'fit',
    autoShow: true,
    initComponent: function () {
        this.items = [
            {
                xtype: 'form',
                bodyStyle: {
                    background: 'none',
                    padding: '10px',
                    border: '0'
                },
                items: [
                    {
                        xtype: 'textfield',
                        name: 'parentId',
                        value: this.getParentId()
                    },
                    {                        
                        xtype: 'textfield',
                        name: 'sifra',
                        allowBlank: false,
                        fieldLabel: 'Šifra'
                    },
                    {
                        xtype: 'textfield',
                        name: 'naziv',
                        allowBlank: false,
                        vtype: 'naziv',
                        fieldLabel: 'Naziv'
                    },

Upvotes: 0

Views: 376

Answers (1)

Rob Schmuecker
Rob Schmuecker

Reputation: 8954

You can either put the variable in the global namespace so that you can reference it from anywhere in the App e.g.

// MyApp would already be initialised in app.js
MyApp.parentId = detailgrid.getParentId();

// then you could reference it anywhere else thereafter
Ext.define('calsberg.view.user.DetailsAdd', {
    extend: 'Ext.window.Window',
    alias: 'widget.detailadd',
    title: 'Add Artikli',
    config:
    {
        parentId: MyApp.parentId
    }
.....

Or you could pass in a config object when you create the widget such as this

var detailgrid = button.up('userdetail');      
var parentId = detailgrid.getParentId();
console.log(parentId);
var view = Ext.widget('detailadd', {config:{parentId:parentId}}); 

Upvotes: 1

Related Questions