Mithril + m.request + Github API

by ramnathv

CoffeeScript

###
Reference: https://github.com/ruanyf/react-demos#demo12-display-value-from-a-promise-source
###
data = 
  items: [
    {name: 'A', html_url: '#', stargazers_count: 20}
    {name: 'B', html_url: '#', stargazers_count: 10}  
  ]

getData = (c) ->
  m.request
    method: "GET"
    url: 'https://api.github.com/search/repositories?q=javascript&sort=stars'
    unwrapSuccess: (response) -> 
      c.loading(false)
      c.data(response.data)
    unwrapError: (error) ->
      c.loading(false)
      c.error(error)

RepoListContainer = 
  controller: ->
    c = this
    loading: m.prop(true)
    data: m.prop([])
    error: m.prop("")

   view: (c) ->
    #m.component(RepoList, {loading: c.loading(), data: c.data()})
    
    
RepoList = 
  view: (ctrl, props) ->
    if props.loading
      m 'span', "Loading..."
    else
      repos = props.data.items
      repoList = repos.map (repo) ->
        m 'li', [
          m "a[href=#{repo.html_url}]", repo.name
          "(#{repo.stargazers_count} stars)"
          m("br")
          repo.description
        ]
      m 'ol', repoList
    
myRepoList = m.component(RepoList, 
 {data: data, loading: false}
)
m.mount(document.body, RepoListContainer)