PDA

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



davidcfk
22 Jul 2013, 12:08 AM
Hello, I have a simple JSON file with structure like so:

{
"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

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


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


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!

davidcfk
22 Jul 2013, 12:13 AM
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:


console.log(record.get("share").name);

Renan Cunha
22 Jul 2013, 2:42 PM
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.


associations: [{ type: 'hasOne', model: "StockManager.model.Share", associationKey: "share", getterName: 'getShare'}]

And then:

record.getShare()

davidcfk
22 Jul 2013, 3:47 PM
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):



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! :)

Renan Cunha
22 Jul 2013, 3:54 PM
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