Debian Codegraph @

dojox/app/utils/mvcModel.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/Deferred", "dojo/when", "dojox/mvc/getStateful"],
2 function(lang, Deferred, when, getStateful){
3 return function(/*Object*/config, /*Object*/params, /*String*/item){
4 // summary:
5 // mvcModel is called for each mvc model, to create the mvc model based upon the type and params.
6 // It will also load models and return the either the loadedModels or a promise.
7 // description:
8 // Called for each model with a modelLoader of "dojox/app/utils/mvcModel", it will
9 // create the model based upon the type and the params set for the model in the config.
10 // config: Object
11 // The models section of the config for this view or for the app.
12 // params: Object
13 // The params set into the config for this model.
14 // item: String
15 // The String with the name of this model
16 // returns: model
17 // The model, of the type specified in the config for this model.
18 var loadedModels = {};
19 var loadMvcModelDeferred = new Deferred();
20
21 var fixupQuery = function(query){
22 var ops = {};
23 for(var item in query){ // need this to handle query params without errors
24 if(item.charAt(0) !== "_"){
25 ops[item] = query[item];
26 }
27 }
28 return(ops);
29 };
30
31 var options;
32 if(params.store){
33 // if query is not set on the model params, it may be set on the store
34 options = {
35 "store": params.store.store,
36 "query": params.query ? fixupQuery(params.query) : params.store.query ? fixupQuery(params.store.query) : {}
37 };
38 }else if(params.datastore){
39 try{
40 var dataStoreCtor = require("dojo/store/DataStore");
41 }catch(e){
42 throw new Error("When using datastore the dojo/store/DataStore module must be listed in the dependencies");
43 }
44 options = {
45 "store": new dataStoreCtor({
46 store: params.datastore.store
47 }),
48 "query": fixupQuery(params.query)
49 };
50 }else if(params.data){
51 if(params.data && lang.isString(params.data)){
52 //get the object specified by string value of data property
53 //cannot assign object literal or reference to data property
54 //because json.ref will generate __parent to point to its parent
55 //and will cause infinitive loop when creating StatefulModel.
56 params.data = lang.getObject(params.data);
57 }
58 options = {"data": params.data, query: {}};
59 }
60 else{
61 console.warn("mvcModel: Missing parameters.");
62 }
63
64 var type = config[item].type ? config[item].type : "dojox/mvc/EditStoreRefListController";
65 // need to load the class to use for the model
66 // modelLoader must be listed in the dependencies and has thus already been loaded so it _must_ be here
67 // => no need for complex code here
68 try{
69 var modelCtor = require(type);
70 }catch(e){
71 throw new Error(type+" must be listed in the dependencies");
72 }
73 var newModel = new modelCtor(options);
74 var createMvcPromise;
75 try{
76 if(newModel.queryStore){
77 createMvcPromise = newModel.queryStore(options.query);
78 }else{
79 var modelProp = newModel._refSourceModelProp || newModel._refModelProp || "model";
80 newModel.set(modelProp, getStateful(options.data));
81 createMvcPromise = newModel;
82 }
83 }catch(ex){
84 //console.warn("load mvc model error.", ex);
85 loadMvcModelDeferred.reject("load mvc model error.");
86 return loadMvcModelDeferred.promise;
87 }
88 when(createMvcPromise, lang.hitch(this, function(){
89 // now the loadedModels[item].models is set.
90 loadedModels = newModel;
91 loadMvcModelDeferred.resolve(loadedModels);
92 //this.app.log("in mvcModel promise path, loadedModels = ", loadedModels);
93 return loadedModels;
94 }), function(){
95 loadMvcModelDeferred.reject("load model error.")
96 });
97 return loadMvcModelDeferred;
98 }
99 });