禁用更新Dojo网格

时间:2015-05-18 21:56:57

标签: javascript dojo

我在Dojo中有四个网格,所有这些网格每隔几秒就会使用实时数据进行更新。

我一次只向用户显示一个网格,但在后台,所有四个网格都会更新。这使得页面在移动设备上变慢了很多(在PC上不明显)。

如果网格不在视图中,是否有办法禁用更新网格?

var myGrid = new (declare([Grid, DijitRegistry]))({
    store: myStore, // this is a Observable(Memory())
    columns:[
        {field: "field1", label: "A", sortable: false},
        {field: "field2", label: "B", sortable: false},
        {field: "field3", label: "C", sortable: false},
        {field: "field4", label: "D", sortable: false},
        {field: "field5", label: "E", sortable: false}
    ],
    selectionMode: "single",
    cellNavigation: true,
    queryOptions: {
        sort:[{attribute: "field1", descending: true}]
    }
},
    myDomRef
);

有类似

的东西
myGrid.disable();

myGrid.enable();

这样只有在启用网格时才会发生DOM更新?

2 个答案:

答案 0 :(得分:1)

如果您使用的是dgrid 0.3.17或dgrid 0.4.0,则可以选择启用/禁用观察,但它们仅在设置商店或集合时生效。

在dgrid 0.3.17中,属性为shouldObserveStore

grid.set('shouldObserveStore', false);
grid.set('store', ...);

在dgrid 0.4.0中,属性为shouldTrackCollection

grid.set('shouldTrackCollection', false);
grid.set('collection', ...);

答案 1 :(得分:-1)

以下是他们关于Observables的文档页面的示例。我没有对此进行测试,但这个想法应该是相同的,你只需要弄清楚细节。

require(["dojo/store/Observable", "dojo/store/Memory"], function (Observable, Memory) {
    var myGrid = new(declare([Grid, DijitRegistry]))({
            store: myStore, // this is a Observable(Memory())
            columns: [
                {
                    field: "field1",
                    label: "A",
                    sortable: false
                },
                {
                    field: "field2",
                    label: "B",
                    sortable: false
                },
                {
                    field: "field3",
                    label: "C",
                    sortable: false
                },
                {
                    field: "field4",
                    label: "D",
                    sortable: false
                },
                {
                    field: "field5",
                    label: "E",
                    sortable: false
                }
            ],
            selectionMode: "single",
            cellNavigation: true,
            queryOptions: {
                sort: [{
                    attribute: "field1",
                    descending: true
                }]
            }
        },
        myDomRef
    );
    // create the initial Observable store
    store = new Observable(new Memory({
        data: someData
    }));

    // query the store
    var results = store.query({
        rating: 5
    });

    // now listen for any changes
    var observeHandle = results.observe(function (object, removedFrom, insertedInto) {
        // do something
    });

    // done observing, any further modifications will not trigger our listener
    observeHandle.cancel();

});