extjs model custom mapp to xml - extjs

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'},

Related

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 models association

Hi StackOverflow users!
I've got some json data, which should be loaded into a model with three assoctiations. Two of them are okey, but third one would not work.
Here's a json reply from server:
http://pastebin.com/geL16BvL
Main Model:
Ext.define('Invoice', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'invoice_id', type: 'string'},
{name: 'invoice_date', type: 'date', dateFormat: 'time'},
{name: 'invoice_due_date', type: 'date', dateFormat: 'time'},
{name: 'invoice_year_index', type: 'int'},
{name: 'invoice_total', type: 'number'},
{name: 'invoice_vat', type: 'number'},
{name: 'invoice_delivery', type: 'number'},
{name: 'invoice_total_cost', type: 'number'},
{name: 'invoice_payment_method', type: 'string'},
{name: 'invoice_status', type: 'string'},
{name: 'invoice_discount', type: 'number'},
{name: 'invoice_discount_type', type: 'string'},
{name: 'invoice_fixed_charges', type: 'number'},
{name: 'invoice_currency_code', type: 'string'},
{name: 'invoice_currency_symbol', type: 'string'},
{name: 'localization_data_source', type: 'string', mapping: 'data_source.data_source_name'}
],
associations: [
{model: 'Contractor', name: 'contractor', type: 'hasOne'},
{model: 'SalesRepresentative', name: 'salesRepresentative', type: 'hasOne', associationKey: 'salesRepresentative'},
{model: 'InvoiceItem', name: 'invoiceItems', type: 'hasMany'}
]
});
Two working:
Ext.define('InvoiceItem', {
extend: 'Ext.data.Model',
belongsTo: 'Invoice',
fields: [
{name: 'id', type: 'int'},
{name: 'invoice_id', type: 'string'},
{name: 'item_variation_id', type: 'string'},
{name: 'item_quantity', type: 'number'},
{name: 'item_name', type: 'string'},
{name: 'item_price', type: 'number'},
{name: 'item_value', type: 'number'},
{name: 'item_position_vat', type: 'number'},
{name: 'item_vat', type: 'number'},
{name: 'item_tax', type: 'number'},
{name: 'item_category_name', type: 'string'},
{name: 'item_discount', type: 'number'},
{name: 'item_price_before_discount', type: 'number'}
]
});
Ext.define('Contractor', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'contractor_id', type: 'string'},
{name: 'contractor_email', type: 'string'},
{name: 'contractor_status', type: 'string'},
{name: 'contractor_registered', type: 'date', dateFormat: 'time'},
{name: 'contractor_name', type: 'string'},
{name: 'contractor_company', type: 'string'},
{name: 'contractor_company_vat_no', type: 'string'},
{name: 'contractor_phone', type: 'string'},
{name: 'contractor_address', type: 'string'},
{name: 'contractor_city', type: 'string'},
{name: 'contractor_postcode', type: 'string'},
{name: 'contractor_country', type: 'string'}
]
});
And Third one - SalesRepresentative:
Ext.define('SalesRepresentative', {
extend: 'Ext.data.Model',
belongsTo: 'Invoice',
fields: [
{name: 'id', type: 'int'},
{name: 'representative_id', type: 'string'},
{name: 'representative_name', type: 'string'},
{name: 'representative_email', type: 'string'}
]
});
When I access Contractor or store with InvoiceDetails anything works really well. But when I try to access SalesRepresentative for example via:
Ext.getStore('invoicesStore').getAt(0).getSalesRepresentative()
I'm receiving
"TypeError: url is
return url + (url.indexOf('?') === -1 ? '?' : '&') + string;"
I'm pretty sure that somethings wrong with relations, but I can't find way to make it work.
Your store probably uses an AJAX proxy (or any other proxy derived from Ext.data.proxy.Server) and tries to load the sales representative record via a server request, but fails to find an URL on the model.
Try adding
proxy: {
type: 'memory'
}
to your model.

Extjs Model Fields with Subfields

Anyone know how you model subfields of any field in ExtJS? For example
Ext.data.Model:
fields:[
{name: 'id', type: 'int'},
{name: 'title', type: 'string'},
{name: 'description', type: 'string'},
{name: 'priority', type: 'auto', fields:[
{name: 'code', type: 'string'}
]},
{name: 'createdBy', type: 'auto'},
]
then in my grid panel
Ext.grid.Panel
columns:[
{header:'Title', dataIndex:'title', flex:1},
{header:'Priority', dataIndex:'code'}
],
Any idea how I access the dataIndex 'code' under 'priority'? thanks in advance!
Thanks to #sha - here is the answer I needed :)
Model
fields:[
{name: 'id', type: 'int'},
{name: 'title', type: 'string'},
{name: 'description', type: 'string'},
{name: 'priority', type: 'auto'},
{name: 'code', type: 'string', mapping:'priority.code'},
{name: 'createdBy', type: 'auto'},
]
Gird Panel
columns:[
{header:'Title', dataIndex:'title', flex:1},
{header:'Description', dataIndex:'description'},
{header:'Priority', dataIndex:'code'}
],
Try this:
dataIndex: 'priority.code'

How to access nested models from store in ExtJs 4

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');

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