-1

I'm using Mithrill v1.0.0, here's the code

var scrollable = {};

scrollable.Product = {
    list : [],
    loadList : function(store){
        m.request({
            method : "GET",
            url : "APIUrl" + store,
            withCredentials : true
        }).then(function(result){
            scrollable.Product.list = result;
        })
    }
}

scrollable.oninit = function(){
    scrollable.Product.loadList("authentic")
}

scrollable.view  = function(){
    console.log(scrollable.Product.list.result) //Here's give me two line log
}

On the console, its give me two line log, the first one is undefined, and the second one an array with API result values.

Kev
  • 15,899
  • 15
  • 79
  • 112
Engkus Kusnadi
  • 2,386
  • 2
  • 18
  • 40

2 Answers2

2

A call to m.request return a promise and trigger a redraw upon completion of its promise chain.

m.request docs

It's drawing twice, once initially and then once again when your m.request completes.

Tivac
  • 2,553
  • 18
  • 21
0

I have never used Mithrill but coming from a ReactJS perspective, I'm guessing that the change in state due to the async call resolution triggers a rerender (i.e. runsscrollable.viewagain).

Here's what I think happens:

  1. Initial state: list is empty

  2. scrollable.oninit runs: async method; does not resolve at once.

  3. scrollable.view runs: console prints still empty list.

  4. async method in 2 resolves. list is assigned fetched values.

  5. State change detected, triggering scrollable.view to run again. Console now prints list with API values.

Rem Lampa
  • 451
  • 6
  • 6