委托 接口
聲明:
/**
* Created by kenny on 16/3/28.
*/
'use strict';
import React, {
Component,
PropTypes
} from 'react';
import {
AppRegistry,
View,
Navigator,
StyleSheet,
StatusBar,
DeviceEventEmitter,
} from 'react-native';
import SplashPage from './pages/SplashPage';
import { naviGoBack } from './utils/CommonUtil';
import LoadingView from './components/LoadingView';
import {STYLESBASE} from './constants/StylesBase';
import MainEnterPage from './pages/MainEnterPage'
let tempNavigator;
let initLoadingState={
isShow:false,
AcIndicShow:true,
text:'數據加載中',
};
class HL_LY_APP_RN extends Component {
constructor(props){
super(props);
this.state = {
LoadingState: initLoadingState,
};
this.init = this.init.bind(this);
this.renderScene = this.renderScene.bind(this);
this.goBack = this.goBack.bind(this);
}
init() {
}
componentWillMount(){
AppController.LoadingState = (LoadingStateData) => {
this.setState({
LoadingState: LoadingStateData
});
}
}
componentDidMount() {
}
componentWillUnmount() {
}
goBack() {
return naviGoBack(tempNavigator);
}
configureScene() {
var conf = Navigator.SceneConfigs.HorizontalSwipeJump;
conf.gestures = null;
return conf;
}
renderScene(route, navigator) {
let Component = route.component;
return <Component {...route.params} navigator={navigator} />
}
render() {
var defaultName = 'Splash';
var defaultComponent = MainEnterPage;
var defaultSceneConfig = Navigator.SceneConfigs.HorizontalSwipeJumpFromRight;
var defaultParams = {};
return (
<View style={{ flex: 1 }}>
<Navigator
style={styles.navigator}
initialRoute = {
{
name: defaultName,
component: defaultComponent,
sceneConfig: defaultSceneConfig,
params: defaultParams
}
}
configureScene={this.configureScene}
renderScene={this.renderScene}
/>
<View accessible={true} style={ {position:'absolute', backgroundColor:'#ffff00', flex:1 }}>
{
this.state.LoadingState.isShow ? <LoadingView text={this.state.LoadingState.text} AcIndicShow={this.state.LoadingState.AcIndicShow} /> : null
}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
navigator: {
flex: 1
}
});
var AppController = {
LoadingState: function(LoadingStateData) {}
}
AppRegistry.registerComponent('HL_LY_APP_RN', () => HL_LY_APP_RN);
var instance = null;
export const AppInstance = function() {
if (instance === null) {
instance = AppController;
}
return AppController;
};
**實現 :
// 請求數據的網絡層提示
AppInstance().LoadingState({
isShow:true,
AcIndicShow:true,
text:'數據加載中',
});
調用:
AppInstance().showLoading(false);
Javascript異步編程的4種方法
http://www.ruanyifeng.com/blog/2012/12/asynchronous_javascript.html
常見的瀏覽器無響應(假死),往往就是因為某一段Javascript代碼長時間運行(比如死循環),導致整個頁面卡在這個地方,其他任務無法執行。
"同步模式"就是上一段的模式,后一個任務等待前一個任務結束,然后再執行,程序的執行順序與任務的排列順序是一致的、同步的;
**"異步模式"則完全不同,每一個任務有一個或多個回調函數(callback),前一個任務結束后,不是執行后一個任務,而是執行回調函數,后一個任務則是不等前一個任務結束就執行,所以程序的執行順序與任務的排列順序是不一致的、異步的。
一、回調函數
function f1(callback){
setTimeout(function () {
// f1的任務代碼
callback();
}, 1000);
}
f1(f2);
二、事件監聽
f1.on('done', f2);
function f1(){
setTimeout(function () {
// f1的任務代碼
f1.trigger('done');
}, 1000);
}
f1.trigger('done')表示,執行完成后,立即觸發done事件,從而開始執行f2。
三、發布/訂閱
我們假定,存在一個"信號中心",某個任務執行完成,就向信號中心"發布"(publish)一個信號,其他任務可以向信號中心"訂閱"(subscribe)這個信號,從而知道什么時候自己可以開始執行。這就叫做"發布/訂閱模式"(publish-subscribe pattern),又稱"觀察者模式"(observer pattern)。
jQuery.subscribe("done", f2);
function f1(){
setTimeout(function () {
// f1的任務代碼
jQuery.publish("done");
}, 1000);
}
jQuery.unsubscribe("done", f2);
四、Promises對象
簡單說,它的思想是,每一個異步任務返回一個Promise對象,該對象有一個then方法,允許指定回調函數。比如,f1的回調函數f2,可以寫成:
**好處:如果一個任務已經完成,再添加回調函數,該回調函數會立即執行。所以,你不用擔心是否錯過了某個事件或信號。這種方法的缺點就是編寫和理解,都相對比較難。
'use strict';
import React, {Component} from 'react'
class NetUitl extends Component {
static request(url, options) {
let isOk;
return new Promise((resolve, reject) => {
fetch(url, options)
.then((response) => {
if (response.ok) {
isOk = true;
} else {
isOk = false;
}
return response.json();
})
.then((responseData) => {
if (isOk) {
resolve(responseData);
} else {
reject(responseData);
}
})
.catch((error) => {
reject(error);
console.log(error);
});
});
}
/**
*url :請求地址
*data:參數(Json對象)
*/
static postJson (url, data) {
var fetchOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
return NetUitl.request(url, fetchOptions);
}
//get請求
/**
*url :請求地址
*/
static get(url) {
return NetUitl.request(url, null);
}
}
module.exports = NetUitl;