UNPKG

rabbitpre-tension-worm

Version:
103 lines (98 loc) 3.24 kB
/** * Created by Administrator on 2015/12/3. */ var _ = require('./utils.js'); var _Source = require('./source.js'); var _Element = require('./element.js'); var beArray = _.beArray; var Count = require('./count.js'); //main function Worm(model,source){ if(!model || !source){ return } this.model = model || {}; var newSource = new _Source(); newSource.setSource(source); this.source = newSource; } Worm.prototype.sourceIntance = null; Worm.prototype.getModel = function(){return this.model;} Worm.prototype.setModel = function(model){this.model = model;} Worm.prototype.getSource = function(){return this.source;} Worm.prototype.setSource = function(source){this.source.setSource(source);} Worm.prototype.build = function(){ var _sources = this.model.sources || []; var _defaults = this.model.default || {}; if(!_sources || !_.isArray(_sources)){ return console.log("[ERROR]:sources must be Array.") } var elements=[]; for(var i=0;i<_sources.length ;i++){ var source = _sources[i]; for(var key in source){ var element = _Element.buildElement(key,source[key],this.source); elements.push(element); //console.log(JSON.stringify(element)); } } //合并所有元素 var instance = _Element.mergeElements(elements); this.sourceIntance = instance; //初始化默认属性 instance = initInstance.call(this,_defaults,instance); return instance; } function initInstance(model,instance){ for(var key in model){ var nodes = key.split('.'); if(nodes.length<1){ continue; } var count = new Count(); init.call(this,instance,nodes,model[key],count); } return instance; } function init(instance,nodes,value,count){ if(_.isArray(instance)){ for(var i=0;i<instance.length;i++){ init.call(this,instance[i],nodes,value,count); } }else if(_.isObject(instance)){ var node = nodes[0]; var checkNode = beArray(node); if(nodes.length == 1){ if(_.isFunction(value)){ var func = value; count.one() if(checkNode){ instance[checkNode]=func(instance[checkNode],count.get(),this.sourceIntance); return }else{ instance[node]=func(instance[node],count.get(),this.sourceIntance); return } }else{ if(checkNode){ if(!instance[checkNode]){ instance[checkNode]=[value]; } return }else{ if(!instance[node]){ instance[node]=value; } return } } } node = checkNode || node; if(!_.has(instance,node)) { instance[node] = {} } init.call(this,instance[node],nodes.slice(1,nodes.length),value,count) }else{ } } module.exports = Worm