如何从RiotControl商店拨打电话?

时间:2016-03-28 03:56:55

标签: ajax riot

我用Nodejs / Express创建了API。 假设我可以向localhost:8080/list执行GET请求,它返回我的TODO列表的JSON,我可以POST到localhost:8080/list来创建新的待办事项列表。

然后我在我的Frontend网站上使用Riotjs + Riotcontrol。 我如何从todostore.js文件请求?

这是我从riotcontrol演示文件夹中获取的riotcontrol todostore.js文件

Riotcontrol

// TodoStore definition.
// Flux stores house application logic and state that relate to a specific domain.
// In this case, a list of todo items.
function TodoStore() {
  riot.observable(this) // Riot provides our event emitter.

  var self = this

  self.todos = [
    { title: 'Task 1', done: false },
    { title: 'Task 2', done: false }
  ]

  // Our store's event handlers / API.
  // This is where we would use AJAX calls to interface with the server.
  // Any number of views can emit actions/events without knowing the specifics of the back-end.
  // This store can easily be swapped for another, while the view components remain untouched.

  self.on('todo_add', function(newTodo) {
    self.todos.push(newTodo)
    self.trigger('todos_changed', self.todos)
  })

  self.on('todo_remove', function() {
    self.todos.pop()
    self.trigger('todos_changed', self.todos)
  })

  self.on('todo_init', function() {
    self.trigger('todos_changed', self.todos)
  })

  // The store emits change events to any listening views, so that they may react and redraw themselves.

}

1 个答案:

答案 0 :(得分:0)

您可以在TodoStore

中执行此类操作
self.on('todo_init', function() {                                
  // Trigger loading here perhaps, then set loading = false when it's loaded
  //self.trigger('set_loading', {value: true})                  
  fetch('http://localhost:8080/list')                        
              .then(response => response.json())                            
              .then(function (json) {                                       
                  self.todos = json                    
                  self.trigger('todos_changed', self.todos) 
              })                                                            
})