0

I created this fiddle showing what I want to do.

Basically, I configured a Store that loads only a single record (it reality it will be the currently signed in user). However, I can't seem to bind anything to that store properly. How can I get that {u.name} to output properly?

Here is the code:

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name']
});

Ext.application({
    name : 'Fiddle',

    launch : function() {

        new Ext.Component({
            renderTo: Ext.getBody(),

            viewModel: {
                stores: {
                    u: {
                        proxy: {
                          type: 'ajax',
                          url: 'user.json'
                        },

                        model: 'User',

                        autoLoad: true
                    }
                }
            },

            bind: 'Test name: {u.name}'
        });

    }
});
Tarabass
  • 3,132
  • 2
  • 17
  • 35
Dave L.
  • 9,595
  • 7
  • 43
  • 69

2 Answers2

4

Try bind: 'Test name: {u.data.items.0.name}'

UPD: As an explanation to this - u is an object with its properties, so trying to bind to u.name will actually bind to the name property of the store. Wile the store has no name property it will always be null. Store's received data is placed in data property. Items property contains a raw array of received data and so on.

yorlin
  • 440
  • 3
  • 15
2

In your fiddle you're using a store while it looks like you need just a single record.

Using links instead of stores should work better, like:

links: {
    u: {
        type: 'User',
        id: 1
    }
}

Working example based on your code: https://fiddle.sencha.com/#fiddle/uuh

CD..
  • 72,281
  • 25
  • 154
  • 163
  • What will happen when user with id=2 will login? How to use links more dynamically? – yorlin Oct 06 '15 at 12:02
  • @CD.. -- Aren't you assuming I would know the `id` of the user who is logged in? This is how the all the docs recommend it is done but it always assumes you would know the `id` somehow – Dave L. Oct 06 '15 at 16:01
  • @yorlin: you can use `linkTo` for setting the link dynamically: http://docs.sencha.com/extjs/6.0/6.0.0-classic/#!/api/Ext.app.ViewModel-method-linkTo – CD.. Nov 08 '15 at 11:53
  • @CD.. Is it possible to set a dynamic url using links as it is done for stores, i.e. proxy: { url: '/base_url/{id}'} ? – fen1ksss Jun 10 '17 at 07:50