jQuery UI Widgets Forums General Discussions Plugins Data Adapter values source field not updated on dataBind()?

This topic contains 4 replies, has 2 voices, and was last updated by  jcwren 10 years, 4 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author

  • jcwren
    Participant

    In the example below, I’m unclear how to cause the playersSource‘s sh_age_fmt values to update when the agesAdapter is updated. When refreshGameData() is called, agesSource.localdata is updated, but when I dump playerSource.datafields [1].values.source, the contents are the original value from the ages array. It appears this might be a copy rather than a reference, otherwise when agesAdapter.records was updated after agesAdapter.dataBind() is called, it would reflect the change?

    At any rate, do you have any suggestions for how to cause the agesAdapter.records used by the playerAdapter to update?

    $(function () {
      'use strict';
    
      var ages = [
        { value: 'ADULT',  label: 'Adult',  },
        { value: 'JUNIOR', label: 'Junior', },
        { value: 'SENIOR', label: 'Senior', },
      ];
    
      var agesSource = {
        datatype: 'array',
        datafields: [
          { name: 'value',   type: 'string' },
          { name: 'label',   type: 'string' },
        ],
        localdata: ages,
      };
    
      var agesAdapter = new $.jqx.dataAdapter (agesSource, {
        autoBind: true
      });
    
      var playerSource = {
        datatype: 'array',
        datafields: [
          { name: 'sh_age',      type: 'string'  },
          { name: 'sh_age_fmt',  value: 'sh_age',   values: { source: agesAdapter.records, value: 'value', name: 'label' }},
        ],
        localdata: [],
      };
    
      var playerAdapter = new $.jqx.dataAdapter (playerSource);
    
      var refreshGameData = function () {
        App.socket.emit ('game:get', function (data) {
          agesSource.localdata = data.gameData.ages;
          agesAdapter.dataBind ();
    
          playerSource.localdata = data.gameData.players;
          $(grid).jqxGrid ({source: playerAdapter});
        });
      };
    });

    Peter Stoev
    Keymaster

    Hi jcwren,

    With this code, they shouldn’t be. If you wish they to be updated, you should set them.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    jcwren
    Participant

    Yes, that’s my question. Within the jqx frankwork, what is the *correct* way to handle that? Something like the following?

    playerSource.datafields [_.findIndex (playerSource.datafields, {name: 'sh_age_fmt'})].values.source = agesAdapter.records;

    Or do you have a more correct suggestion?


    Peter Stoev
    Keymaster

    Hi jcwren,

    It would be better if you move your adapters creation code within 1 function and call it when you need your data updated so it will return new adapter instance each time. Otherwise, Yes, you should update the values, because the adapter cannot know that you changed some Array somewhere in your code.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    jcwren
    Participant

    OK, I can do that.

    There are other places in the various widgets where you can pass either a jqxDataAdapter or an array. If would be nice if the source field could handle that also. That way, regardless of when the agesAdapter is updated, any time the playerSource referenced the sh_age_fmt field, it would pick up the most current agesAdapter records. This would also be more consistent with the way it appears dataAdapters are intended to be used.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.