How to set current item in App Maker datasource?

前端 未结 2 1258
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 11:26

This seems basic but I can\'t seem to figure out how to manually set the current item to work with from the datasource?

To illustrate: I have a table and I notice th

相关标签:
2条回答
  • 2021-01-20 11:40

    Why it happens:

    AM code: generate button click event
    User code: handle button's click event
    User code: navigate user to different page
    AM code: destroy DOM of current page
    AM code: build DOM for new page
    -- dead code after this line
    AM code: row click event handler
    AM code: change datasource's current item
    

    Row's click event handler never gets control, because row is destroyed by user's code.

    What Morfinismo's solution does?

    AM code: generate button click event
    User code: handle button's click event
    AM code: row click event handler
    AM code: change datasource's current item
    -- moved lines
    User code: navigate user to different page
    AM code: destroy DOM of current page
    AM code: build DOM for new page
    

    Here are more tech details: Event Loop

    In App Maker this problem can be solved with

    1. setTimeout
    2. Force current item update in user code
    // button's onClick event handler
    app.datasource.ListDatasource.selectKey(widget.datasource.item._key);
    
    1. CustomProperties
    // button's onClick event handler
    app.pages.ShowMeNext.properties.Key = widget.datasource.item._key;
    app.showPage(app.pages.ShowMeNext);
    
    // next page's onAttach event handler
    app.datasources.RecordDatasource.filters._key._equals = app.currentPage.properties.Key;
    app.datasources.RecordDatasource.load();
    
    1. URL parameters and history - this approach is used in most template apps, because it also implements deep linking on some extent.
    // button's onClick event handler
    var params = {
                   key: widget.datasource.item._key
                 };
    var page = app.pages.ShowMeNext;
    app.showPage(page);
    google.script.history.replace(null, params, page.name);
    
    // next page's onAttach event handler
    google.script.url.getLocation(function(location) {
      app.datasources.RecordDatasource.filters._key._equals = location.parameters.key;
    });
    
    1. Passing value between pages using global scope
    // button's onClick event handler
    window.key = widget.datasource.item._key;
    
    // next page's onAttach event handler
    app.datasources.RecordDatasource.filters._key._equals = window.key;
    

    ListDatasource - list/grid/table datasource

    RecordDatasource - datasource dedicated for a specific record (single-record datasource)

    0 讨论(0)
  • 2021-01-20 11:40

    Use a timeout function. This happens because it takes some time for appmaker to change the item in the datasource. You can use something like this on the onClick event handler of the button or link that will take you to the other page:

    setTimeout(function(){
      app.showPage(app.pages.pageToNavigate);
    },200);
    

    That should take care of the issue. I hope this helps!

    0 讨论(0)
提交回复
热议问题