PDA

View Full Version : Ext Js Model Associations issue



abhinavchugh
5 Jun 2012, 4:25 AM
Hi

I am trying to use the Ext.data.model Associations.I took the example from sencha docs and I m getting the error "url is undefined".
Please help me out:-


Ext.define('Category', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int', defaultValue:'22' },
{ name: 'name', type: 'string', defaultValue:'him' }
]
});

Ext.define('Product', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'category_id', type: 'int' },
{ name: 'name', type: 'string' }
],
// we can use the belongsTo shortcut on the model to create a belongsTo association
associations: [
{ type: 'belongsTo', model: 'Category', name: 'category' }
]
// we can use the belongsTo shortcut on the model to create a belongsTo association
});

var product = new Product({
id: 100,
category_id: 20,
name: 'Sneakers'
});

product.getCategory(function(category, operation) {
// do something with the category object
alert(category.get('id')); // alerts 20
}, this);

skirtle
5 Jun 2012, 11:04 AM
Have you tried adding a suitable proxy to the Category model? From the docs:


The getCategory function was created on the Product model when we defined the association. This uses the Category's configured proxy to load the Category asynchronously, calling the provided callback when it has loaded.

el_chief
5 Jun 2012, 11:35 AM
Rules for Belongs To Associations:

http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html

abhinavchugh
6 Jun 2012, 12:12 AM
can't i use the model wothout proxy..??

el_chief
6 Jun 2012, 10:14 AM
You can, but why?

Your store will inherit the model's proxy anyways.

You can always override it.

Putting proxy in the model lets you load one model at a time with the model.load() static method

Putting the proxy in the model lets you have multiple stores with the same model without having to set their proxies.

skirtle
6 Jun 2012, 12:15 PM
@abhinavchugh. There are many options here. Perhaps if you could tell us what you want it to do?

Currently what's happening is you're defining a model called Product which belongs to a Category. You're then creating an instance called product. However, product just has the category's id (20), it doesn't actually have the category record.

(The term record is used to refer to an instance of a model, in case that isn't clear).

When you call the getCategory method on product it tries to resolve the Category from its id. It does that by trying to load it through the proxy specified on the Category.

If you don't want it to load the Category through the proxy then what do you want it to do? It's got to get Category 20 from somewhere.