MedryxORM and dot-path notation
Another nifty feature of MedryxORM is dot-path support. Since this ORM is all about relationships beween domain objects, it only makes sense to give the you access to traversing your relationship paths. This is used in two places primarily: getValue and properties. Let me give you a couple examples.
Lets say you are making a table of patients. You want to display their name, room number, and insurance. The object model looks something like:
In javascript, you would represent this model as:
dojo.declare("model.BaseEntity", null, { id:new medryx.orm.Identifier() }); dojo.declare("model.Patient", model.BaseEntity, { firstName:new medryx.model.Property({label:2}), lastName:new medryx.model.Property({label:1}), visit:new medryx.model.Association({associationClass:"model.Visit"}) getDisplayName:function() { return this.getLastName() + ", " + this.getFirstName(); } queries:[ {name:"AllPatients", properties:["*", "visit.*"]} ] }); dojo.declare("model.Visit", model.BaseEntity, { room:new medryx.model.Property(), bed:new medryx.model.Property(), insurance:new medryx.model.Association({associationClass:"model.Insurance") }); dojo.declare("model.Insurance", model.BaseEntity. { companyName:new medryx.model.Property(), groupNumber:new medryx.model.Property() }); entityManager.registerClass("model.Patient", "Patient"); entityManager.registerClass("model.Visit", "Visit"); entityManager.registerClass("model.Insurance", "Insurance");
So after running all this code, you now have a managed model that you can query (assuming you have set up your perisistenceService) and display the results, lets say in a grid. So lets define a grid:
var view = { cells:[ [ {name:"Name", field:"displayName"}, {name:"Room", field:"visit.room"}, {name:"Bed", field:"visit.bed"}, {name:"Insurance", field:"visit.insurance.companyName"}, ]] }; var grid = new dojox.grid.DataGrid({ store:entityManager, query:{}, queryOptions:{entityClass:"Patient", namedQuery:"AllPatients", properties:dojo.map(view.cells[0], function(column) { return column.field; })}, structure:[view] }, dojo.byId("patientsGridNode");
And that is it. So what have we done here?
As you can see, I’ve declare 3 simple classes. Each has some simple properties, and for kicks, I’ve declared a simple getDisplayName() method on my Patient.
I’ve defined a named query that get all patients from the server, and for each, returns all properties of the Patient and the patient’s associated Visit. (Note that this means the server will return the id of the insurance association, but not any attributes of the insurance company (i.e. the companyName and the groupNumber are not returned.)
Next we declare a simple dojox.grid.DataGrid. (This uses the grid model from Dojo 1.2, where DojoData has been removed and essentially plunked into DataGrid itself.)
As you can see, the “field” name in the view attached to the grid’s stucture uses “dot-path” notation to get to the desired field to display. Since our query is returning Patient’s, we need to navigate from the patient to the desired display field. This fails gracefully, retuning null if any part of the path does not exist.
Now lets look at the queryOptions in the grid. First, see how there is no query? I don’t absolutely have to have one — which means by default I will show all patients. The queryOptions are used to tell the entityManager which query to run. We then send in a new list of properties for the entityManager. Since we know that we will certainly need all the displayed fields in our data set, we can simply map() the view into a list of properties that we would like to instruct the entityManager to fetch.
Here is where things get really nice. Without any work on your part, the entityManager will have only made one call to the server when all of this is done, and it will have all the data you need to display your grid.
Now lets say you wanted to create a “detail” view that allowed people to see the patient’s insurance groupNumber if they clicked on the insurance information. Simple!
dojo.connect(grid, "onRowClick", function(e) { var patient = grid.getItem(e.rowIndex); alert("GroupNumber: " + patient.getVisit().getInsurance().getGroupNumber()); });
Notice how there is no query here, or anything else for you to write! You just use your getter, and you get lazy initialization of the groupNumber for free!
