PDA

View Full Version : What Is The Benefit of Model Associations?



arivwd
23 Dec 2014, 6:11 PM
Is it recomended to use associations in model?
What is the benefit of model associations?

jasewell
24 Dec 2014, 8:40 AM
Model associations only come into play if you're using data that is structured that way. If you do have data that naturally breaks up into things that "have" and/or "belong to" other things, using associations can save you time and code in the following ways:

You don't have to juggle multiple store variables to get at related records. For example, if you have a User model that hasMany Posts and Comments, and you want to display that User's recent Posts and Comments, rather than getting a reference to the stores for Posts and Comments and filtering them both, you can use userRecord.posts() and userRecord.comments() to access just the records that have the same user_id.
You can load related data all at once. If your server-side provider formats the data correctly, you can load all three of those stores at the same time, by nesting Posts and Comments under each User record. See the page for Ext.data.reader.Reader for an example of this kind of nested loading.
That's it. If those things seem like they'll be easier than doing things the long way, then yes, using associations is recommended.