Results 1 to 5 of 5

Thread: hasOne associations, but no idea on why it couldn't detect Object on ajax load

  1. #1
    Sencha User
    Join Date
    Jul 2013
    Posts
    5

    Default Answered: hasOne associations, but no idea on why it couldn't detect Object on ajax load

    Hello, I have a simple JSON file with structure like so:
    Code:
    {
        "success" : true,
        "data" : [{
            "share": {
                data: {
                    "name": "CCB",
                    "code": "HK.0939"
                }
            },
            "amount": '5000',
            "price": '5.0'
        }]
    }
    Basically I have a Portfolio (which extends a store) that I can add many stocks to (another model).
    The Stock contains a hasOne reference to a Share (which only contains the name and the code).

    Now if I were to have the following for the Portfolio.js
    Code:
    Ext.define("StockManager.store.Portfolio", {    
        extend: "Ext.data.Store",
        model: "StockManager.model.Stock",
        autoreload: true,
        proxy: {
            type: "ajax",
            url: "resources/db/data.json",
            reader: {
                type: 'json',
                root: 'data'
            }
        }
    });
    And for Stock.js
    Code:
    Ext.define("StockManager.model.Stock", {
        extend: "Ext.data.Model",
        fields: [
            {name: "share", type: "auto"},
            {name: "price", type: "float"},
            {name: "amount", type: "int"}
        ],
        associations: [{ type: 'hasOne', model: "StockManager.model.Share", associationKey: "share"}],
        validations: [
            {type: "presence", field: "share"},
            {type: "presence", field: "price"},
            {type: "presence", field: "amount"}
        ]
    });
    and finally the simple Share.js
    Code:
    Ext.define("StockManager.model.Share", {
        extend: "Ext.data.Model",
        fields: [
            {name: "code", type: "string"},
            {name: "name", type: "string"}
        ],
        validations: [
            {type: "presence", field: "code"},
            {type: "presence", field: "name"}
        ]
    });
    I noticed that after I can get access to the share object using the get("share"), but because it wasn't aware that Share is an Ext object it doesn't know how to process get("share").get("name").

    I wonder what I am missing here...? I couldn't find a good coverage for the hasOne or get other similar examples working on this.

    Many thanks!
    Last edited by davidcfk; 22 Jul 2013 at 1:13 AM. Reason: Formatting issues. Made it easier to read...

  2. Hello!

    You can try to configure the getterName of this association and then use the method that will be created for access the associated object.

    Code:
    associations: [{ type: 'hasOne', model: "StockManager.model.Share", associationKey: "share", getterName: 'getShare'}]
    And then:
    Code:
    record.getShare()

  3. #2
    Sencha User
    Join Date
    Jul 2013
    Posts
    5

    Default

    Not wrapping the share's data in a data field kind of helped the cause a little bit. But still couldn't get access to the name/code using the get("") convention...

    But it works if I use something like:
    Code:
    console.log(record.get("share").name);

  4. #3
    Sencha User
    Join Date
    Dec 2010
    Location
    SC - Brazil
    Posts
    27
    Answers
    3

    Default

    Hello!

    You can try to configure the getterName of this association and then use the method that will be created for access the associated object.

    Code:
    associations: [{ type: 'hasOne', model: "StockManager.model.Share", associationKey: "share", getterName: 'getShare'}]
    And then:
    Code:
    record.getShare()

  5. #4
    Sencha User
    Join Date
    Jul 2013
    Posts
    5

    Default

    Thanks Renan!

    So as it turns out I followed your suggestion, it worked. Though now I had to drop the share field since the hasOne creates another object with it's own data. The original thought I had meant I can create a stock like so (which works if you are creating it, but doesn't work for me when loading from JSON via ajax):

    Code:
    var stock = Ext.create("StockManager.model.Stock", {
        share: Ext.create("StockManager.model.Share", {
            code: "AX.ASX",
            name: "ASX"
        },
        amount: 100,
        price: 10.0,
    });
    
    // then access by
    stock.get("share").get("name");
    
    // in contrast with
    var stock = Ext.create("StockManager.model.Stock");
    stock.setStock(Ext.create("StockManager.model.Share, {code: "AX.ASX", name: "ASX"});
    
    // access by
    stock.getShare().get("name");
    Running getShare() on it returns the share object as expected. (and works when retrieving from the JSON file as well).

    It's a little bit inconsistent with what I'm trying to do but I suppose it works for now until a better explanation of how I can do this comes along, cheers!

  6. #5
    Sencha User
    Join Date
    Dec 2010
    Location
    SC - Brazil
    Posts
    27
    Answers
    3

    Default

    Yeh! The documentation/examples about the ext js associations aren't so enlightening until now but if you follow the stack of calls in the source code you will have a better situation about this feature. Lets waits some big examples from the Sencha =P

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •