//cloud js
export default{
  data(){
    return {
      newName:'',
      item:{
        ready:false
      }
    };
  },
  props:['_name','_action'],
  mounted(){
    this.init();
    //onez.data(this,['items']);
  },
  watch:{
    data(){
      this.init();
    }
  },
  methods:{
    init(){
      this.newName=this._name;
      if(typeof(Vue.component(this.newName))==='function'){
        this.output();
      }else{
        onez.cloudList=onez.cloudList||{};
        if(typeof onez.cloudList[this.newName]!='undefined'){
          onez.cloudList[this.newName].push(this);
          return;
        }
        onez.cloudList[this.newName]=[];
        onez.post({
          $action:this.action,
          $file:this.path,
          _name:this.newName,
          _action:this._action,
          action:'data'
        },(res)=>{
          if(res.cssUrl){
            onez.$('<link rel="stylesheet" type="text/css" href="'+res.cssUrl+'" />').appendTo('body');
          }
          if(res.css){
            onez.$('<style type="text/css">'+res.css+'</style>').appendTo('body');
          }
          if(res.jsUrl){
            that.loadJS(res.jsUrl,this.newLoaded);
          }else if(res.js){
            eval(res.js);
            this.newLoaded();
          }
        });
      }
    },
    output(){
      this.item.is=this.newName;
      this.item.data=this.data;
      this.item.ready=true;
    },
    newLoaded(){
      if(typeof onez.cloudList[this.newName]!='undefined' && typeof onez.cloudList[this.newName]!='undefined'){
        for(var i=0;i<onez.cloudList[this.newName].length;i++){
          onez.cloudList[this.newName][i].output();
        }
        delete onez.cloudList[this.newName];
      }
      this.output();
    }
  }
}
