2015-07-21 36 views
1

我卡試圖弄清楚如何寫一個通量商店,從我的使用altjs創建從API

import $ from 'jquery'; 

const utils = { 

myProfile:() => { 
    return $.ajax({ 
     url: '/myProfile', 
     type: 'GET' 
    }); 
    } 
}; 

這是快遞API在短短獲取數據工作的行動獲取數據的altjs通量店我相信我應該寫我的GET請求只抓取用戶的配置文件(這應該返回一個帶有用戶信息的json)。

然後我的商店:

import UserActions from 'actions/UserActions'; 
import alt from 'altInstance'; 
class UserStore { 

constructor() { 
    this.userProfile = []; 
    this.on('init', this.bootstrap); 
    this.on('bootstrap', this.bootstrap); 
    this.bindListeners({ 
    fetchUserProfile: UserActions.FETCHUSERPROFILE, 
    }); 
    } 

fetchUserProfile(profile) { 
    this.userProfile = profile; 
    } 
} 
export default alt.createStore(UserStore, 'UserStore'); 

但是動作就是我是最無能的

import alt from 'altInstance'; 
import UserWebAPIUtils from 'utils/UserWebAPIUtils'; 
fetchProfile(){ 
    this.dispatch(); 
    UserWebAPIUtils.getProfile() 
     //what do we do with it to let our store know we have the data? 
     }); 
     } 
     } 
    } 

的所有IM試圖做的,是從服務器獲取數據,告訴我的商店我們已經收到數據並用我們的api中的數據填充用戶配置數組,並且用於告訴我們的商店是通過屬於「操作」正確的調度員的信使?我已經看了很多教程,但對於我如何思考這個問題我仍然沒有很自信。如果我想通過POST請求更新數據會怎麼樣?

回答

3

翻閱altjs文檔,似乎他們建議從操作中進行異步操作。我更喜歡這種方法,因爲它使商店保持同步並且易於理解。基於它們的例子

LocationAction

LocationsFetcher.fetch() 
    .then((locations) => { 
    // we can access other actions within our action through `this.actions` 
    this.actions.updateLocations(locations); 
    }) 
    .catch((errorMessage) => { 
    this.actions.locationsFailed(errorMessage); 
    }); 

基本上它們獲取的信息,然後觸發2分的動作取決於在其上存儲被偵聽到該請求的結果。

LocationStore

this.bindListeners({ 
    handleUpdateLocations: LocationActions.UPDATE_LOCATIONS, 
    handleFetchLocations: LocationActions.FETCH_LOCATIONS, 
    handleLocationsFailed: LocationActions.LOCATIONS_FAILED 
}); 

當商店接收handleUpdateLocations動作恰好當提取器返回成功。該商店將更新自己的新數據和調度

handleUpdateLocations(locations) { 
    this.locations = locations; 
    this.errorMessage = null; 
} 

隨着你的代碼,你可以做類似的事情。當原始請求數據時,獲取用戶配置文件將被觸發。在這裏,我將用戶配置文件設置爲[],這是您原始的init值,但您可以將其設置爲任何值以指示數據正在加載。然後我添加了2個方法,handleFetchUserProfileComplete和handleFetchUserProfileError,根據您的提取是否成功進行調用。下面的代碼是粗糙你應該有什麼想法。

constructor() { 
    this.userProfile = []; 
    this.on('init', this.bootstrap); 
    this.on('bootstrap', this.bootstrap); 
    this.bindListeners({ 
     handleFetchUserProfile: UserActions.FETCH_USER_PROFILE, 
     handleFetchUserProfileComplete: UserActions.FETCH_USER_PROFILE_COMPLETE, 
     handleFetchUserProfileError: UserActions.FETCH_USER_PROFILE_ERROR, 
    }); 
    } 

fetchUserProfile() { 
    this.userProfile = []; 
} 

handleFetchUserProfileComplete(profile) { 
    this.userProfile = profile; 
} 

handleFetchUserProfileError(error) { 
    this.error= error; 
} 

export default alt.createStore(UserStore, 'UserStore'); 

剩下的唯一的事情是取決於你的行動代碼的讀取請求的結果

fetchUserProfile(){ 
    this.dispatch(); 
    UserWebAPIUtils.getProfile().then((data) => { 
     //what do we do with it to let our store know we have the data? 
     this.actions.fetchUserProfileComplete(data) 
    }) 
    .catch((errorMessage) => { 
     this.actions.locationsFailed(errorMessage); 
    }); 
} 

fetchUserProfileComplete(profile) { 
    this.dispatch(profile); 
} 

fetchUserProfileError(error) { 
    this.dispatch(error); 
} 
+0

非常感謝你,這是真的清楚觸發這兩個動作,所以我只是要檢討它又一次1.我們通過初始化一個空數組來設置我們的存儲區(就像我們將會做出反應一樣),然後我們有一些東西來處理獲取用戶配置文件的操作,但是由於它的異步我們需要數據結構我們開始,當我們結束時,當然有一個錯誤,如果有事情中斷。然後在我們的行動中,我們從那裏調度並調用我們的API。由於我的ajax調用它會被.done和.fail而不是.then和.catch,但如果它是一個es6的承諾,那會是對的? – Karan

+1

你的理解聽起來很合理。當數據/錯誤返回或最初請求數據時,商店會記錄調用的回調函數。該操作最初調用API,並通知商店加載請求已進入,然後稍後調度由AJAX請求返回的數據或錯誤,這些回調已被註冊。是的,我想如果你使用jQuery的Ajax然後.done和.fail是正確的方法。 – noveyak

+0

其實他們建議把它放在商店:http://alt.js.org/docs/async/ @noveyak – Burimi