How to Create List of String in a Model : Extjs - extjs

How do i make a field as List of String elements while creating a Ext.data.Model in Ext js?
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int', convert: null},
{name: 'phone', type: 'string'},
{name: 'alive', type: 'boolean', defaultValue: true}
]
});

Just exclude the type:
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'foo'}
]
});
var o = new User({
foo: ['a', 'b', 'c']
});
console.log(o.get('foo'));

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

extjs4 grid grouped with function

Ext.define('record.SecurityCase', {
extend : 'Ext.data.Model',
fields : [{
name : 'id',
type : 'string',
persist : false
}, {
name : 'takeplaceTime',
type : 'date',
persist : true
}, {
name : 'brief',
type : 'string',
persist : true
}],
groupField: 'takeplaceTime'
});
A grid with store used model above.And I want it to be grouped with 'takeplaceTime'.for example:
obj.takeplaceTime='2013-10-01';obj2.takeplaceTime='2013-04-01';obj3.takeplaceTime='2013-12-01';obj4.takeplaceTime='2012-12-01';
obj;obj2;obj3 are one group as its year is 2013. obj4 will be another ,year 2012.
Is there a groupRenderFunction to handle this?
You can define calculated property in Model for grouping. Example:
Ext.define('Model1', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'seniority', type: 'string'},
{name: 'department', type: 'string'},
{name: 'group', type: 'string', convert: function(value, record) {
return record.get('name').substr(0, 1); // first letter of name
} }
]
});
Working sample in Ext JS 4.2: http://jsfiddle.net/BmVeg/1/

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.

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

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

Resources