Results 1 to 9 of 9

Thread: Binding to array item via index?

  1. #1
    Sencha Premium Member
    Join Date
    Aug 2014
    Posts
    39
    Answers
    2
    Vote Rating
    4
      1  

    Default Answered: Binding to array item via index?

    Hi, is it actually possible to bind to an array item via index etc.?

    Just tried the following which doesn't work.

    ViewModel:

    Code:
    ...
    data: {
        myarray: [
            { text: 'a' },
            { text: 'b' }
        ]
    }
    ...

    View:

    Code:
    ...
    bind: {
        text: {'myarray[0].text'}
    }
    ...

  2. No, you would need to use a formula.

  3. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,165
    Answers
    746
    Vote Rating
    988
      2  

    Default

    No, you would need to use a formula.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  4. #3
    Sencha User
    Join Date
    Jan 2013
    Posts
    3
    Vote Rating
    7
      5  

    Default You can

    write this

    bind: { text: '{myarray.0.text}' }

  5. #4
    Sencha Premium Member
    Join Date
    Aug 2014
    Posts
    39
    Answers
    2
    Vote Rating
    4
      1  

    Default

    @Geddeon: Oh cool. This seem to work indeed. Even changing the value works with viewModel.set('myarray.0.text', 'XXX'). Thank you.

    Are there any downsides of this? Somehow it feels like the "non-supported" (but working) way?

  6. #5
    Sencha User
    Join Date
    Jan 2013
    Posts
    3
    Vote Rating
    7
      2  

    Default

    No there is no any downsides, but I'm confused why sencha dev team doesn't say anything about it. I saw many questions regarding array binding in the forum, but in all questions sencha dev team says that it not possible .....

  7. #6
    Sencha User alfonso.nishikawa's Avatar
    Join Date
    Jul 2015
    Posts
    94
    Answers
    2
    Vote Rating
    27
      0  

    Default

    -deleted-

  8. #7
    Sencha User
    Join Date
    Jun 2017
    Posts
    5
    Vote Rating
    2
      1  

    Default

    I have found an issue using bind: { text: '{myarray.0.number}' }

    Let's say we have the following viewModel
    Code:
    ...
    data: {
        myarray: [
            { number: 5 },
            { number: 10 }
        ]
    }
    ...


    When you bind this value to numberfield or another field and change this value, your viewModel will look like

    Code:
    ...
    data: {
        myarray: {
            0: {
                number: 5
            }
        }
    }
    
    ...
    Two important things here:
    1. Now you don't have an array here. This is an object with first key '0' which also is an object.
    2. You lost your data except the data you have changed.
    Fiddle example here: https://fiddle.sencha.com/#view/editor&fiddle/25kj .

  9. #8
    Sencha User alfonso.nishikawa's Avatar
    Join Date
    Jul 2015
    Posts
    94
    Answers
    2
    Vote Rating
    27
      1  

    Default

    @Dimitrii: thank you for pointing it out. This are not good news for me I have some code that will depend on this :S ¿Any workaround? ¿Any dirty fix around?

  10. #9
    Sencha User alfonso.nishikawa's Avatar
    Join Date
    Jul 2015
    Posts
    94
    Answers
    2
    Vote Rating
    27
      1  

    Default

    I made some modifications to the core and seems to work...
    I am using 6.0.1.250 (sorry for the ones using newer versions).

    The modifications are the following three, all to Ext.app.bind.Stub (Stub.js).

    At method trackHadValue:
    Code:
    // old
    // if (value && (value.constructor === Object || value.isModel)) {
    // fixed
    if (value && (value.constructor === Object || value.constructor === Array || value.isModel)) {

    At method getDataObject:
    Code:
    // old
    // if (!ret || !(ret.$className || Ext.isObject(ret))) {
    // fixed
    if (!ret || !(ret.$className || Ext.isObject(ret) || Ext.isArray(ret))) {
    At method set:
    Code:
    // old
    // } else if ((value && value.constructor === Object) || value !== parentData[name]) {
    // fixed
    } else if ((value && (value.constructor === Object || value.constructor === Array)) || value !== parentData[name]) {

    Modifying the core is not a good practice, so I suggest creating an Override, or just installing this package I created for 6.0.1.250: https://bitbucket.org/alfonsonishikawa/extjspackages/wiki/Escaped%20binds

    If anyone needs to know how to create an override, the source code is here: https://bitbucket.org/alfonsonishika...e-view-default

    Maybe this screws up other things :P For me it works by now...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •