autobots-lib
Version:
汽车人基础库
102 lines (78 loc) • 1.92 kB
JavaScript
;
import React,{Component} from 'react';
import ReactNative from 'react-native';
import Native from '../native';
import Toast from './toast';
import PropTypes from 'prop-types';
const {
StyleSheet,
requireNativeComponent,
NativeModules,
DeviceEventEmitter,
Platform,
View,
Text,
} = ReactNative;
let received_emps = null;
let openNative = function(requestUrl,funcname,eventname,params,callback)
{
let url = 'autobots://rn/getData?url='+requestUrl+'&funcname='+funcname+'&event='+eventname;
if(params)
{
url = url + '&'+params;
}
Native.callNative(url,function(result,data){
if(callback)
{
callback(result,data);
}
});
}
class Me extends Component {
request(callback){
openNative(this.props.url,this.props.funcName,this.props.eventName,this.props.params,callback);
}
componentWillMount() {
var that = this;
function cb_android(e:Event)
{
received_emps = JSON.parse(e.result);
if(that.props.onPicked)
{
that.props.onPicked(JSON.parse(e.result));
}
}
function cb_ios(p)
{
received_emps = p;
if(that.props.onPicked)
{
that.props.onPicked(p);
}
}
if(Platform.OS==='android')
{
this.sinListener = DeviceEventEmitter.addListener(this.props.eventName,cb_android);
}else {
this.sinListener = DeviceEventEmitter.addListener(this.props.eventName,cb_ios);
}
}
componentWillUnmount() {
if(this.sinListener!=null)
{
this.sinListener.remove();
this.sinListener = null;
}
}
render() {
return(<View/>);
}
}
Me.propTypes={
onPicked: PropTypes.func.isRequired,
url: PropTypes.string.isRequired,
funcName: PropTypes.string.isRequired,
eventName:PropTypes.string.isRequired,
params:PropTypes.string.isRequired,
}
module.exports = Me;