日期:2014-05-16  浏览次数:20363 次

ExtJs4-ModelFactory模型工厂
Ext.define("factory.ModelFactory",{
        statics:{
                //数据类模型的集合
                models:new Ext.util.MixedCollection(),
                //字段集合
                fields:new Ext.util.MixedCollection(),                
                getModelByName:function(modelName,excludes){
                        //不需要的字段
                        var nofields = {};
                        if(excludes){
                                nofields = {"excludes":excludes};
                        }
                        //1.声明类,返回类的ClassName
                        if(!this.models.containsKey(modelName)){
                                //ajax拿到我们的字段集合
                                var fields = [];
                                if(this.fields.containsKey(modelName)){
                                        fields = this.fields.containsKey(modelName)
                                }else{
                                        Ext.Ajax.request({
                                                url:'/pc/doAction!getModelFields.action?modelName='+modelName,
                                                method:'POST',
                                                params:nofields,
                                                timeout:4000,
                                                async:false,//跟关键 我不需要异步操作
                                                success:function(response,opts){
                                                        fields = eval(response.responseText);
                                                }
                                        });
                                }
                                this.fields.add(modelName,fields);
                                
                                var newModel = Ext.define(modelName,{
                                        extend:'Ext.data.Model',
                                        fields:fields
                                });
                                this.models.add(modelName,newModel);
                        }
                        
                        return {modelName:modelName,model:this.models.get(modelName)};
                }
        }
});