2016-03-28 48 views
0

我已經用Nodejs/Express創建了API。 讓我說我可以做localhost:8080/list的GET請求,它返回我的TODO列表的JSON,我可以發佈到localhost:8080/list創建新的待辦事項列表。如何從RiotControl Store ajax調用?

然後我用我的前端網站Riotjs + Riotcontrol。 如何從todostore.js文件請求?

這是todostore.js文件riotcontrol這是我從riotcontrol演示文件夾獲取

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. 

} 

回答

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) 
       })                
})