PDA

View Full Version : Question on associations



horcle_buzz
23 Feb 2013, 12:57 PM
Phew... 3.5 weeks of confusion, and I am now fully convinced that ExtJS rocks (fought with learning curve issues, bugs, features, etc.), but I survived (and thus, I will not cop out and take the easy/alternate route of writing my app in JQM/Backbone/Lawnchair/Cordova/Backbone-Associations). =P~

That being said, my question is more about a design issue I have encountered in trying to model a complex relationship between 3-classes (data models) in an MVC Javascript framework.

I have the following 3 models:

Site:


Ext.define('EvaluateIt.model.Site', {
extend: 'Ext.data.Model',

config: {
//idProperty: 'id', // use with proxy.SQL
identifier: 'uuid', // use with proxy.localstorage
fields: [
{name: 'remoteSiteId', type: 'int'}
],
proxy: {
type: "localstorage"//,
//database: 'Yo'
},
hasMany: [
{
model: 'EvaluateIt.model.Evaluation',
name: 'siteEvaluations',
associationKey: 'evaluation_id'
}
],
/*hasMany: [{ model: 'EvaluateIt.model.SiteMaintainer', name: 'siteMaintainers' }],
hasOne: [{ model: 'EvaluateIt.model.Geolocation' }],*/
hasOne: [
{
model: 'EvaluateIt.model.Address'
}
]
}
});


Evaluation:



Ext.require(['Ext.data.proxy.SQL','EvaluateIt.model.Site','EvaluateIt.model.Evaluator']);

Ext.define('EvaluateIt.model.Evaluation', {
extend: 'Ext.data.Model',

config: {
//idProperty: 'id', // use with proxy.SQL
identifier: 'uuid', // use with proxy.localstorage
fields: [
{name: 'remoteEvaluationId', type: 'int'},
//{name: 'sumRating', type: 'int'},
//{name: 'dateloadedToDevice', type: 'date'},
//{name: 'datePostedToRemote', type: 'date'},
{name: 'dateUpdated', type: 'date'},
//{name: 'dateOfEvaluation', type: 'date'},
//{name: 'noLongerExists', type: 'boolean'},
//{name: 'comments', type: 'string'},
{name: 'evaluationType', type: 'int'}
],
proxy: {
type: "localstorage"//,
//database: 'Yo'
},


belongsTo: [
{
model: 'EvaluateIt.model.Site',
associationKey: 'siteId',
name: 'site',
instanceName: 'site',
getterName: 'getSite',
setterName: 'setSite',
foreignKey: 'site_id'
},
{
model: 'EvaluateIt.model.Evaluator',
associationKey: 'evaluator_id',
name: 'evaluator',
instanceName: 'evaluator',
getterName: 'getEvaluator',
setterName: 'setEvaluator',
foreignKey: 'evaluator_id'
}
]


// hasOne: [{ model: 'EvaluateIt.model.EvaluationAward' }],
// hasMany: [{ model: 'EvaluateIt.model.EvaluationFactorScorecard' }],
// hasMany: [{ model: 'EvaluateIt.model.EvaluationFeature' }],
}
});


And Evaluator:




Ext.define('EvaluateIt.model.Evaluator', {
extend: 'Ext.data.Model',

config: {
//idProperty: 'id', // use with proxy.SQL
identifier: 'uuid', // use with proxy.localstorage
fields: [
{name: 'remoteEvaluatorId', type: 'int'},
{name: 'firstName', type: 'auto'},
{name: 'lastName', type: 'auto'},
{name: 'email', type: 'auto'}
],
proxy: {
type: "localstorage"//,
//database: 'Yo'
},


hasMany: [{ model: 'EvaluateIt.model.Evaluation', name: 'evaluatorEvaluations', associationKey: 'evaluation_id' }]


}
});




The associations of interest, are in summary:

One Site can have many Evaluations
One Evaluator can have many Evaluations

I am easily able to make one or the other of these linked associations, but am having a heckuva time with both associations.

If I were doing this in a Java'ish-like environment, I would solve this by just doing something like

class Evaluation {
evaluator Evaluator

hasMany: { site: Site}

}

but, alas, I have no idea how to achieve this in a Javascript MVC?

A friend suggested using a linking table, but this seems to be overly complex to utilize.

Any suggestions?

Thanks!

Greg--

mitchellsimoens
25 Feb 2013, 7:36 AM
What does your data look like?

horcle_buzz
27 Feb 2013, 6:42 PM
Hi Mitchell,
I have to admit that this was a crazy idea on my part. :">

In a nutshell: Yes, I am dealing with JSON in both directions, so I have no idea why I used a model with classes to represent something that is not easily representable as a JSON object. In JSON, my association is actually that "an Evaluation hasOne Evaluator" (while in reality an evaluator can/and does indeed have multiple evaluations, try as I might, I canot come up with a JSON representation of this that makes sense).

For you edification, here is how my JSON looks, with the attribute names remapped for the remote RESTfull service to handle (so, yes, I was crazy in terms of my original design!):


evaluation: {"evaluation_id":44214,
"score":18,
"rating":"EG",
"rating_year":2012,
"bestof":"Residential Raingarden",
"special_award_specified":"",
"nate_siegel_award":0,
"score_card":{"color":3, "plant_variety":4, "design":3, "maintenance":4, "environmental_ stewardship":4},
"date_evaluated":"2012-08-03",
"general_comments":"test",
"evaluator":{"evaluator_id":"265", "completed_by":"265", name:"me"},
"rainbarrel":0,
"raingarden":1,
"garden":{"garden_id":39439, "no_longer_exists":1,
"address":{"neighborhood":null, "county":"Hennepin"},
"gardener":{"name":"test"},
"geolocation":{"latitude":44.9267," longitude":-93.405282, "accuracy":"42000"}
}
}]

So, in summary, thanks for smacking me upside the head with the actual model! :)