How to access nested models from store in ExtJs 4 - extjs

I just downloaded the final version of ExtJs 4 and I'm trying to implement some things using new Model approach.
For instance I have a model named SetupModel, it has 2 nested models Users, Reports.
I create new store and I set the Model property of the store = SetupModel.
The question is - how can I access my nested properties after data was loaded into the store?
I need something like myStore.data.Users(), but is incorrect.
Any thoughts?

When you define your model, you need to provide the necessary association with the nested models. Since, you have not provided your code. Here is an example:
My Product Model:
Product = Ext.define('Product',{
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'user_id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'price', type: 'float'}
],
proxy: {
type: 'localstorage',
id: 'products'
}
});
My User Model:
User = Ext.define('User',{
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'gender', type: 'string'},
{name: 'username', type: 'string'}
],
associations: [
{type: 'hasMany', model: 'Product', name: 'products'}
],
proxy: {
type: 'localstorage',
id : 'users'
}
});
Now, if you have a instance of User model with products. Here is how you can access the products:
var productStore = user.products();
Note that user.products() returns a Ext.data.Store. Now, you can traverse or filter or find your product record. Here is how I got my first product's name:
productStore.getAt(0).get('name');

Related

ExtJS localstorage proxy model/store issue

I'm using ExtJS 5.1.0 and running into a strange problem.
I have the following model:
Ext.define('cardioCatalogQT.model.Load', {
extend: 'Ext.data.Model',
config:{
idProperty: 'id',
fields: [
{name: 'key', type: 'string'},
{name: 'comparator', type: 'string'},
{name: 'value', type: 'string'},
{name: 'type', type: 'string'}
]
}
});
And the Store:
Ext.define('cardioCatalogQT.store.Payload', {
extend: 'Ext.data.Store',
alias: 'store.Payload',
// add package.framework=ext to .sencha/app/sencha.cfg
config:{
model: 'cardioCatalogQT.model.Load',
storeId: 'Payload',
autoLoad: true,
proxy: {
type: 'localstorage'
}
}
});
When I add a record to the store:
var payload = Ext.create('cardioCatalogQT.store.Payload');
payload.add({
type: 'dx',
key: item.data.code,
comparator: 'eq',
value: item.data.description
});
payload.sync();
The data in localstorage look like all the records except for #7 and #11 in the attached image
However, when I add the field elements to the store like this:
Ext.define('cardioCatalogQT.store.Payload', {
extend: 'Ext.data.Store',
alias: 'store.Payload',
// add package.framework=ext to .sencha/app/sencha.cfg
config:{
idProperty: 'id',
fields: [
{name: 'type', type: 'string'},
{name: 'key', type: 'string'},
{name: 'comparator', type: 'string'},
{name: 'value', type: 'string'}
] ,
//model: 'cardioCatalogQT.model.Load',
storeId: 'Payload',
autoLoad: true,
proxy: {
type: 'localstorage'
}
}
});
I get the desired results in 7 and 11.
One other issue, is instead of giving an actual name to the model record, I get the very not useful name of ext-data-proxy-localstorage-N for the model instance.
Also, I am getting this error when referencing the model via the store:
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (cardioCatalogQT.model.Load, line 0)
I have done a sencha app build and no errors were thrown.
Any idea as to what I am doing wrong (I am basically doing what I've done with Sencha Touch, where I had much success)? Is this a bug, perhaps?
Issue was for ExtJS, to get the desired name on the store, you have to specify and id in the proxy:
proxy: {
type: 'localstorage',
id : 'payload'
}
Now, the model instances in my localstorage are showing up as desired, and I can query them using standard methods, like findRecord. Good times!
Moral of the story: ExtJS has different behaviors than Touch

idProperty in Sencha Touch 2.3.1

I have the model:
Ext.define('EvaluateIt.model.Address', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{name: 'address', type: 'string'},
{name: 'city', type: 'string'},
{name: 'state', type: 'string'},
{name: 'zipcode', type: 'string'},
{name: 'county', type: 'string'}
],
proxy: {
type: "sql",
database: 'Test'
}
}
});
In my controller, I insert data into this model as follows:
var address = Ext.create('EvaluateIt.model.Address', {
address: json[i].garden.address.address
});
address.save();
Where the json array is grabbed via Ajax and thus inserted into the model without any problems.
However, when I try to access the id from the model like,
console.log('address.id ' + address.id);
I get something of the form ext-record-n (where n does not even map to the id in my Address table). How can I reference the actual value of the id column in this table? I tried this, but was unsuccessful: https://github.com/tomalex0/SenchaTouch-v2-SqliteProxy/issues/3
Adding the id field to the fields array will cause the proxy to create the database table with column id as an AUTOINCREMENT field.
Ext.define('EvaluateIt.model.Address', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'address', type: 'string'},
{name: 'city', type: 'string'},
{name: 'state', type: 'string'},
{name: 'zipcode', type: 'string'},
{name: 'county', type: 'string'}
],
proxy: {
type: "sql",
database: 'Test'
}
}
});
The proxy will read the id value created by the database. You can access the value by passing a callback function to the model save method.
var address = Ext.create('EvaluateIt.model.Address', {
address: json[i].garden.address.address
});
address.save(function(record) {
console.log('address.id ' + record.getId();
}, this);

extjs model custom mapp to xml

My model has clientName model/AlertConfig.js:
Ext.define('ET.model.AlertConfig', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'clientName', type: 'string' },
{name: 'description', type: 'string'},
Incoming xml has the whole client object embedded (if present):
<alertConfig>
<commandType>D</commandType>
<countThreshold>1</countThreshold>
<description>Distributions that failed(all clients)</description>
<id>2</id>
<processTimeExceedsSec>0</processTimeExceedsSec>
<status>3</status>
<windowMinutes>120</windowMinutes>
</alertConfig>
<alertConfig>
<client>
<clientCategory>abc</clientCategory>
<clientExtId>12345</clientExtId>
<clientId>1</clientId>
<clientName>MyClient1</clientName>
<lastModified>2013-03-19T16:12:54.774-04:00</lastModified>
</client>
<commandType>R</commandType>
<countThreshold>1</countThreshold>
<description>Requests that failed</description>
<id>3</id>
<processTimeExceedsSec>0</processTimeExceedsSec>
<status>3</status>
<windowMinutes>120</windowMinutes>
</alertConfig>
QUESTION:
I need only the alertConfig/client/clientName to be mapped to clientName in model. How can I do it cleanly. Is there a place where I can mention this xml path?
Note: In the view User will see the ClientName (and all clientNames in a combobox, user may choose&update with a different clientName for the alert.)
Resolved by adding '/'
Ext.define('ET.model.AlertConfig', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'clientId', mapping: 'client/clientId', type: 'int' },
//not a field in erf.domain (but points to Client object)
{name: 'clientName', mapping: 'client/clientName', type: 'string' }, // not a field in erf.domain
{name: 'description', type: 'string'},

Belongs to association in Sencha

How can i fetch a record using belongs to association ?
var Category = Ext.regModel('Category', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
});
var Product = Ext.regModel('Product', {
fields: [
{name: 'id', type: 'int'},
{name: 'category_id', type: 'int'},
{name: 'name', type: 'string'}
],
associations: [
{type: 'belongsTo', model: 'Category'}
]
});
Amol,
I speak only of ExtJS 4, so if you can relate this to ExtJS 3 then great, but you would retrieve multiple records via a store:
As a starting point though you could do
var records = Ext.StoreManager.lookupStore("productStore").load({
belongTo: 'books',
callback: function(records) {
Ext.each(records,function(record) {
console.log(JSON.stringify(record.raw))
});
}
});
Depending on the proxy setup on the productStore, this will append belongTo to the querystring so if reader proxy has url: /products and the extra params belongTo is sent this will hit the server with /products?belongTo=books

extjs post with parameter

I'm creating a data store that will load the data from server. I'm wondering how I can pass parameters to the proxy.
var dataStore = new Ext.data.JsonStore({
proxy:'productSearch.php',
root:'products',
fields:['title', 'image', 'inStock', 'price', 'category', 'manufacturer']
});
I usually do it like this
var dataStore = new Ext.data.JsonStore({
url: 'productSearch.php'
root: 'products',
baseParams: { //here you can define params you want to be sent on each request from this store
param1: 'value1',
param2: 'value2'
},
fields: [...]
});
dataStore.load({
params: { //here you can define params on 'per request' basis
param3: 'value3'
}
});
I also prefer to define fields like this:
fields: [
{name: 'title', mapping: 'title', type: 'string'},
{name: 'image', mapping: 'image', type: 'string'},
{name: 'inStock', mapping: 'inStock', type: 'bool'},
{name: 'price', mapping: 'price', type: 'float'},
{name: 'category', mapping: 'category', type: 'int'},
{name: 'someDate', mapping: 'someDate', type: 'date', dateFormat: 'Y-m-d'},
]
Two things here:
I assign the types, so that the store is loaded with correct datatypes. It will even convert string dates to JavaScript Date() objects.
I use 'mapping' parameter, to tell which fields from JSON are to be matched to which fields in the store. If for whatever reason JSON format changes, I only need to make one change here.

Resources