1. #1
    Ext JS Premium Member
    Join Date
    May 2011
    Posts
    12
    Vote Rating
    0
    atrius is on a distinguished road

      0  

    Default Understanding StoreModel Associations

    Understanding StoreModel Associations


    So what I'm curious about right now is how to define a store/model association that is singular. Ie: not hasMany but only has one. Technically I can get around this by saying making a hasMany relation and only providing one thing but I was just curious if there is a better way of defining this.

    For example, I have a report for a company. On the report is the company address information. Also on the form is multiple customer information.

    So a Report Model would have many customer addresses that are 1 model and is a hasMany relation but would also have a company address model that would only be 1 of. As I type this I realize that I used addresses for both but let's assume that they are different models for some reason or another. 1 Company per report and multiple Customers. Plus various fields for the report itself thus.

    Report Model->


    fields : [{various fields here....}],
    associations: [
    {type: 'hasMany', model: 'Company'},
    {type: 'hasMany', model: 'Customer',}
    ]

    is this actually how I should be representing it despite the fact the model should only have 1 company reference?

  2. #2
    Sencha User
    Join Date
    Apr 2010
    Posts
    78
    Vote Rating
    1
    kskrumpet is on a distinguished road

      0  

    Default


    The easy answer to the question is there isn't a better way of specifically doing what you want to do, but there is a better way of doing what you're trying to do.

    If there's only going to be one company then why can't the company fields be rolled into the top level model? Even if this is not how your database is structured behind the scenes, surely the script that supplies the data can provide a flat-mapped record with all the information?

  3. #3
    Ext JS Premium Member
    Join Date
    May 2011
    Posts
    12
    Vote Rating
    0
    atrius is on a distinguished road

      0  

    Default


    because there are many different reports, all of which have different fields but all have companyinfo. In my mind it makes more sense to have the companyinfo in a seperate model to refernce to since it gets reused a lot. That is just my normal view of OOP though.

    I did think of doing it your way. Just personally don't care for it. I don't think either of these is particularly great but they are a big deal either. I was just trying to get a better understanding of how/why associations are only by hasMany and belongsTo. This is an example where neither really apply but due to reuse you want to separate Company from the rest.

    If there is a reason to go one way or the other not just from a design point (performance or some such) I would like to hear that as well.

  4. #4
    Sencha User
    Join Date
    Apr 2010
    Posts
    78
    Vote Rating
    1
    kskrumpet is on a distinguished road

      0  

    Default


    I'm not denying you need to do this on the server side, but questioning whether you really need to mimic the data source structure with the view you're trying to create. Think of it like a database - you'd probably want to create a view for this; that is, something that looks like a table but actually pulls the company details in from the right table to show alongside the reports. In the same way, if all your app is doing is loading these reports then why not flatten the model?

    If your app is doing something else, then it sounds like you need two stores, one for companies and one for reports, with a link from a report to a company, by id or whatever, rather than a nested model.

    I've never tried this, so would be interested to know, but can you do the following?

    Code:
        Ext.define('Model1', {
          extend: 'Ext.data.Model',
          fields: [
            {name: 'id'},
            {name: 'item', type: 'Model2'}
          ]
        });
    Rich

  5. #5
    Ext JS Premium Member
    Join Date
    May 2011
    Posts
    12
    Vote Rating
    0
    atrius is on a distinguished road

      0  

    Default


    I have no idea if you can do that, but that is basically what I wanted to do.

    Also part of the reasoning is that the company model already exists for other uses anyways. Why add all these fields into a new thing in the report models over and over? Just seems a waste. I understand you can flatten it but that just seems a waste of time coding wise when I could just as easily add the association. One line for what could be 30 fields.

  6. #6
    Sencha User
    Join Date
    Apr 2010
    Posts
    78
    Vote Rating
    1
    kskrumpet is on a distinguished road

      0  

    Default


    Also part of the reasoning is that the company model already exists for other uses anyways.
    Ah, yes, if that's the case then you don't really want to be mucking about with nesting the data as a hasMany, nor flattening the structure!

    I've had a play and it doesn't seem to be as easy. The model allows you to use "Model2" as a type, but I can't get it to load the data or link to the data in the right way, so it looks like you'd also need to provide "Model1" with some access functions that allow you to link through. It shouldn't be hard.

Thread Participants: 1

Tags for this Thread

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar