[dojox/app] How to separate model definition into a file?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[dojox/app] How to separate model definition into a file?

PaulChristopher
Views have templates, controllers, and models. In appconfig.json, one could define the path to the file representing the template and the controller. However the model needs to be passed in as a variable.

I would like to put the model definition into a separate file so that in the view folder, you have a controller.js, a view.html and a model.js file.

How can I do this?

Looking at the testcase "simpleModelApp", all the models (or their raw data) is created in the top level file "modelApp.js". Inside "modelApp.js", I would now try to load a model definition with require(["dojo/text!myModel.js"] and then parse the text using JSON.parse and put the result into the very same global object....

But all in all, I wonder, if not a generic approach to put models into a separate file could be added to dojox/app? Or maybe it is already there and I have not found it?

Or do I fundamentally misunderstand the concept of models?
Reply | Threaded
Open this post in threaded view
|

Re: [dojox/app] How to separate model definition into a file?

edchat2
The assumption on models is that the data used to create the model will come from a Dojo Store.  We use static data for our tests to keep things simple, (we don't want to have require a backend service) so for many tests the static data is used by a Memory store which is used to create the model.  But we do have some examples of other stores being used.  "simpleModelApp" also has an example using a "dojo/store/JsonRest" which loads it's data from a target file.  Maybe that example gives you what you want?
 

On Tue, Mar 26, 2013 at 7:52 AM, PaulChristopher <[hidden email]> wrote:
Views have templates, controllers, and models. In appconfig.json, one could
define the path to the file representing the template and the controller.
However the model needs to be passed in as a variable.

I would like to put the model definition into a separate file so that in the
view folder, you have a controller.js, a view.html and a model.js file.

How can I do this?

Looking at the testcase "simpleModelApp", all the models (or their raw data)
is created in the top level file "modelApp.js". Inside "modelApp.js", I
would now try to load a model definition with
require(["dojo/text!myModel.js"] and then parse the text using JSON.parse
and put the result into the very same global object....

But all in all, I wonder, if not a generic approach to put models into a
separate file could be added to dojox/app? Or maybe it is already there and
I have not found it?

Or do I fundamentally misunderstand the concept of models?




--
View this message in context: http://dojo-toolkit.33424.n3.nabble.com/dojox-app-How-to-separate-model-definition-into-a-file-tp3995281.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.
________________________________________________________
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/
Reference Guide: http://dojotoolkit.org/reference-guide
API Documentation: http://dojotoolkit.org/api

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


________________________________________________________
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/
Reference Guide: http://dojotoolkit.org/reference-guide
API Documentation: http://dojotoolkit.org/api

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: [dojox/app] How to separate model definition into a file?

PaulChristopher
Mmmh, not sure. In the appconfig.json file, I would like to write something like this:

...
"views": {
    "home": {
        "dependencies": [...],
        "template": "./home/view.html",
        "controller": "./home/controller.js",
        "model": {
            "names": {
                 "modelLoader": "dojox/app/utils/mvcModel",
                 "type": "dojox/mvc/ModelRefController",
                 "params":{
                     "class": "./home/model.js"
                 }
            }
        }
    },
...

Whereas ./home/model.js is an AMD module like this:

define([
	"dojo/_base/declare",
	"dojo/Stateful"
], function(declare, Stateful){

             // Return a class. Could also return a singleton?
             return declare(Stateful, {
                  myprop1: 5,
                  myprops2: ...

                  _setMyProps3Attr: function () {
                      // A custom setter
                      ....
                      this._set('myProps3', value);
                  },

                  constructor: function(){
                      this.myprops3 = [1, 2, 3, 4];		
                  }
             });
});

Dojox/app should now create an instance of this model and use it. Do I need to write a new model loader for that in dojox/apps/util?

Or do you think, I fundamentally misunderstand the concept of models?

In the "home" view folder, you got now a view.html, a controller.js and a model.js file. And this is an improvement in the overall code structure/ readability of a project, IMO.
Reply | Threaded
Open this post in threaded view
|

Re: [dojox/app] How to separate model definition into a file?

edchat2
I see, mvcModel does not have support for a "class" param, it has support for "store", "dataStore" and "data".  So you can create your own "modelLoader" if you want to take a "class" and use it as you are describing. 

On Thu, Mar 28, 2013 at 12:01 PM, PaulChristopher <[hidden email]> wrote:
Mmmh, not sure. In the appconfig.json file, I would like to write something
like this:



Whereas ./home/model.js is an AMD module like this:



Dojox/app should now create an instance of this model and use it. Do I need
to write a new model loader for that in dojox/apps/util?

Or do you think, I fundamentally misunderstand the concept of models?

In the "home" view folder, you got now a view.html, a controller.js and a
model.js file. And this is an improvement in the overall code structure/
readability of a project, IMO.



--
View this message in context: http://dojo-toolkit.33424.n3.nabble.com/dojox-app-How-to-separate-model-definition-into-a-file-tp3995281p3995381.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.
________________________________________________________
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/
Reference Guide: http://dojotoolkit.org/reference-guide
API Documentation: http://dojotoolkit.org/api

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


________________________________________________________
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/
Reference Guide: http://dojotoolkit.org/reference-guide
API Documentation: http://dojotoolkit.org/api

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: [dojox/app] How to separate model definition into a file?

PaulChristopher
Ed, I have added a patch for that, see http://bugs.dojotoolkit.org/ticket/16946 . Both, simpleModel and mvcModel now allow to load a model based on a mid.