1

I have a view:

Ext.define('DemoApp.view.Main', {
    //Some code here
    ...
    items: [{
        xtype: 'label',
        text: //<- I want to run some scripts then return a string to this.
    }]
})

I tried:

text: function() {
    return 'Test';
}

but nothing happened.

Please help!

1
  • Can you define the object and then, afterwards, run whatever code it is you want to fill the text field? Like items[0].text = 'Test'. As it stands, it seems that you are just setting text to be a function and not executing that function. Commented Nov 20, 2015 at 11:12

3 Answers 3

2

Solution 1:

function getText(){
   if(someVariable==someValue){
     return 'text1';
   } else{
     return 'text2';
   }
};
Ext.define('DemoApp.view.Main', {
    //Some code here
    ...
    items: [{
        xtype: 'label',
        text: getText()
    }]
});

In this case, if you are writing the main definition as a separate file, getText function will become global function.

Solution 2:

Ext.define('DemoApp.view.Main', {
    //Some code here
    ...
    initComponent: function(){
        function getText(){
            if(someVariable==someValue){
               return 'text1';
            } else{
               return 'text2';
            }
        };
        this.items = [{
           xtype: 'label',
           text: getText()
        }];
        this.callParent(arguments);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Thank you very much!
Worth noting that Ext.define accepts an anonymous function as an optional third parameter - allowing you to neatly resolve any imperative logic without polluting the global scope.
0

One set of parentheses can make so much difference:

text: function() {
    return 'Test';
}()

Comments

-1

It works for me please check the below code.

 <script>
    function returntext(d){
        return d;
    }
    window.item = [{
        text: returntext("Name")
    }]
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.