/* * @Author: 陶秋峰 * @Date: 2016-01-06 22:55:03 * @Last Modified by: 陶秋峰 * @Last Modified time: 2016-01-06 22:55:32 * @CopyRight 蛮蛮工作室 */ import global from './global'; import {Hash} from './interfaces'; const url = global.url || 'http://127.0.0.1/get-data'; export enum enum_param_relation{ and, or } export enum enum_param_op{ _in, not_in, like, not_like, contains } export interface ICondition{ op: enum_param_op; name: string; value: string | number | boolean | IParam; } export interface IParam{ rel: enum_param_relation; conditions: ICondition[]; } export interface IData_def{ name: string; modelid: string; editable: boolean; table_name: string; param: IParam; sort: Hash; paging_size: number; paging_index: number; auto_load_data: boolean; } import request from './request'; import Promise from './promise'; export default class Data{ private _idx = -1; public get index(): number { return this._idx; } public set index(idx: number) { this._idx = idx; } public reset(){ this._idx = -1; } public next() { return ++this._idx < this.row_count; } public get row_count(): number { let data = this.data; return (data && data.length) || 0; } private def: IData_def; public get auto_load_data(){ return this.def.auto_load_data; } public get_row(idx: number = this._idx): any { return this.data[idx]; } private data: any[]; public load_data(){ this.data = []; return new Promise((resolve, reject)=>{ let id = this.def.modelid; if(!id){ reject('model id is required'); return; } request.post(url, { responseType: 'json', data: {} }) .then((data) => { if (data.data) { let d = <{ code: number; data: any[]; err_msg: string; }>data.data; if(d.code === 1){ this.data = d.data; resolve(d.data); } else { reject(d.err_msg); } } else { reject(data.statusText); } }, function(e){ reject(e); }) }); } public constructor(def: IData_def){ this.def = def; } }