Dojo Javascript variable scope

2019-01-29 10:23发布

问题:

I am using dgrid and i am attempting to set the dataStore externally. When the page loads i call aliasTicket.load() to create the grid. At the time the grid is loading the datasource is null. When a query is executed the setAliasSource(aliasData); is set.

There are no errors however the grid is still empty. The aliasStore is being updated with data however it isn't being reflected on the grid even after the grid is refreshed. How can i get the data reflected in the grid after the query?

Javascript Object

   var aliasTicket = (function (){

    var aliasData = [];

    require([ "dojo/store/Observable", "dojo/store/Memory"]);   
    var aliasStore = new dojo.store.Observable(new dojo.store.Memory({
        data: aliasData,
        idProperty: "id"
      }));  

    return{         

        load:function(){

            require([
                     ........

                   ], function(declare, Memory, OnDemandGrid, ColumnSet, Selection,
                     selector, Keyboard, DijitRegistry, editor, ColumnHider,
                     registry, Observable,lang) {

                aliasData = this.aliasData;

                var Store = this.aliasStore = new dojo.store.Observable(new dojo.store.Memory({
                    data: aliasData,
                    idProperty: "id"
                  }));

                console.log(Store);

                var CustomAliasNameGrid = declare([OnDemandGrid, selector, Selection, Keyboard, editor, DijitRegistry, ColumnHider]);

                  var aliasNameGrid = new CustomAliasNameGrid({
                    store: Store,
                    columns: {
                      id: {
                        label: "Id",
                        field: "id",
                        hidden: true,
                        autoSizeColumn: true
                      },

                      employeeTicketId: {
                        label: "Employee Ticket Id",
                        field: "employeeTicketId",
                        hidden: true,
                        autoSizeColumn: true
                      },

                      chkBox: selector({}),


                      aliasName: {
                        label: "Alias Names",
                        field: "aliasTicketName",
                        autoSizeColumn: true,
                        formatter: function(str) {
                          return str.replace(/\w\S*/g, function(txt) {
                            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
                          });
                        }
                      }

                    },
                    selectionMode: "none",
                    loadingMessage: "Loading data...",
                    noDataMessage: "No results found....",
                    allowSelectAll: true
                  }, "aliasNameGrid");  



                aliasNameGrid.refresh()

            }); 

        },

        setAliasSource: function (data){
            console.log(data);
            this.aliasSource = data;            

        },

        setAliasData: function (data){
            this.aliasData = data;              
        },

        getAliasSource: function (){

            return this.aliasSource;
        }       

    };  

})();

Setting Data Store Data

aliasData = [{.....},
             {.....},
             {......];          


            require(["dijit/dijit"]);
            aliasTicket.setAliasSource(aliasData);              
            dijit.byId('aliasNameGrid').refresh();

回答1:

You are setting 'this.Store' to an object array, not a real 'dojo store' object. Following your code I can not see where you actually use 'this.Store'. Inside the grid code I do see a local variable named 'Store'.

So I'm not sure if I'm following your code example here but, you should 'set' the store of the grid and then refresh it. Something like this.

 setAliasSource: function (data){
        console.log(data);
        this.Store = data;
        dijit.byId('aliasNameGrid').set("store",new dojo.store.Observable(new dojo.store.Memory({ data: data,idProperty: "id"}));
        dijit.byId('aliasNameGrid').refresh();

    },