Extjs get Model by Controller - extjs

I get controller bind to model, and model with proxy on it. I try to get model in controller init function. I use next code:
Ext.define("AM.controller.Main", {
extend: 'Ext.app.Controller',
views: [
'Grid'
],
models:[
'AM.model.Points'
],
init: function(){
var m = this.getModel('AM.model.Points');
m.getCount();
}
But the getCount is not defined. When I print variable m it says it is function: constructor() :
function constructor() {
return this.constructor.apply(this, arguments) || null;
}
What should I do to get right model object of controller?
Here I add my store, my model with proxy on it:
Ext.define('AM.store.ServerStore', {
extend: 'Ext.data.Store',
model: 'AM.model.Points'
});
Ext.define('AM.model.Points', {
extend: 'Ext.data.Model',
idProperty: {
name: 'UUID',
type: String,
isUnique: true
},
fields: [
{
name: 'NO',
type: "string"
},
{
name: 'Y',
type: "int"
},
{
name: 'ROW',
type: 'int'
},
{
name: 'SEAT',
type: 'string'
},
{
name: 'PROCEEDED',
type: 'int'
},
{
name: 'X',
type: "int"
},
{
name: "CurrentPlace",
type: "int",
defaultValue: 0
}
],
proxy: {
type: 'ajax',
url: 'http://localhost:81/is-bin/INTERSHOP.enfinity/WFS/EnterpriseOrg-MainChannel-Site/ru_RU/-/RUR/HallScheme-PrepareHSXML?HallSchemeElementUUID=zm1VFZxy014AAAENz2ZCr4MF',
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
params: {
"HallSchemeElementUUID" : "zm1VFZxy014AAAENz2ZCr4MF"
},
remoteSort: true,
reader: {
type: 'xml',
record: 'PLACE'
},
writer: {
type: 'xml',
root: 'PLACE'
}
}
});

Models are used to defined a record structure in a store. The getCount() does not exist for the Model class in ExtJS.
Instead, you need to be referencing your store in your controller that contains your models and call getCount() on the store instance.
E.g.
var store = this.getStore('storeName');
//or
//var store = Ext.getStore('storeID');
var numRecords = store.getCount();

Related

ExtJs-> Ext.Store.proxy.writer error: No type specified for writer.create

So I have upgraded from extjs4 to extjs6 and this store always have this error:
No type specified for writer.create
This code has two uses:
For getting the data for the initial page
For getting the data for whenever a button is clicked.
Whenever I comment out the proxy.writer code portion, it will produce the data for the initial page.
But whenever I won't comment it out, it will not get the data for the initial page.
And it will also return this error whenever I clicked a button:
Uncaught TypeError: items.slice is not a function
So my question is, is the writer portion have wrong syntax or something since it is updated to extjs6?
P.S. I tried to change the
this.callOverridden to this.callParent since it states that the this.callOverridden is already deprecated, still has the same error.
Ext.define('Stm.store.stmpdate', {
extend: 'Extends.data.Store',
requires: [
'Cstm.Setting',
'Stm.model.stmpdate'
],
model: 'Stm.model.stmpdate',
pageSize: Stm.Const.controller.dataCountLimit,
remoteSort: true,
proxy: {
type: 'ajax',
url: Cstm.Setting.getEntryUrl() + '/stm/stm-update/stm-update',
reader: {
type: 'json',
rootProperty: 'data'
},
writer: Ext.data.writer.Json.override({
getRecordData: function() {
var data = this.callOverridden(arguments);
var record = arguments[0];
if ( record.associations.length > 0 ) {
Ext.Array.each(record.associations.keys, function(key) {
data[key] = [];
var assocStore = record[key]();
Ext.Array.each(assocStore.data.items, function(assocItem) {
data[key].push(assocItem.data);
});
});
}
return data;
}
}),
api: {
create: Cstm.Setting.getEntryUrl() + '/stm/stm-update/application',
update: Cstm.Setting.getEntryUrl() + '/stm/stm-update/approval'
},
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST',
destroy: 'POST'
}
},
sorters: [
{property: 'aplDatetime', direction: 'DESC'},
{property: 'siteDomain', direction: 'ASC'},
{property: 'pageName', direction: 'ASC'}
]
});
Model:
Ext.define('Stm.model.stmpdate', {
extend: 'Ext.data.Model',
fields: [
{name: 'siteId', type: 'integer'},
{name: 'siteName', type: 'string'},
{name: 'siteUrl', type: 'string'},
{name: 'tmpId', type: 'integer', defaultValue: 1},
{name: 'updType', type: 'string'}
],
hasMany: [{
model: 'Stm.model.ServerInfo',
name: 'servers',
associationKey: 'servers',
reference: 'tmpId'
}]
});
Yes, you are using override in a way that may have worked in ExtJS 4 but is AFAIK unsupported across all versions of ExtJS.
What you want to do is define your custom writer as a new class:
Ext.define('MyApp.app.data.MyCustomJsonWriter', {
extend: 'Ext.data.writer.Json',
alias: 'writer.mycustomjson',
getRecordData: function() {
var data = this.callParent(arguments);
var record = arguments[0];
if ( record.associations.length > 0 ) {
Ext.Array.each(record.associations.keys, function(key) {
data[key] = [];
var assocStore = record[key]();
Ext.Array.each(assocStore.data.items, function(assocItem) {
data[key].push(assocItem.data);
});
});
}
return data;
}
});
require that class from your store:
requires: [
'MyApp.app.data.MyCustomJsonWriter'
]
and then instantiate it by alias:
writer: {
type: 'mycustomjson'
}

Sencha Touch: reference not generated

I'm using Sencha Touch 2.4, with Sencha Cmd 6.1.x (so I believe I'm using Ext JS 6). I've got the following model and store:
Ext.define('App.model.User', {
extend: 'Ext.data.Model',
fields: [{
name: 'organizationId',
type :'int',
reference: {
type: 'Organization',
association: 'UsersByOrganization',
role: 'organization',
inverse: 'users'
}
}, {
"name": "matricola",
"type": "int"
}]
});
and
Ext.define('App.model.Organization', {
extend: 'Ext.data.Model',
fields: ['name']
});
I load my stores (with a 'sql' proxy) using the usual way, but I cannot find my reference anywhere. I simply get the records and I cannot call "users" or its inverse.
Any idea?
Sencha Touch 2.4 and ExtJS 6 are two different toolkits. Syntax for creating models and stores are similar in both, but not in all cases.
I believe what you are looking for is the StoreManager. If you have defined a store like so:
Ext.define('App.store.User', {
extend: 'Ext.data.Store',
storeId: 'User',
model: 'User'
});
Then you can reference the store like so:
// Return a list of users
Ext.getStore('User').getRange();
The code below works for me on Ext JS 6. Maybe you can model yours after this example:
Ext.define('App.model.Customer', {
extend: 'Ext.data.Model',
idProperty: 'customerNumber',
fields: [
{ name: 'customerNumber', type: 'int' },
{ name: 'customerName', type: 'string' },
{ name: 'contactLastName', type: 'string' },
{ name: 'contactFirstName', type: 'string' }
],
proxy: {
type: 'ajax',
url: '../../../api/CustomersAssociationsReference',
reader: {
type: 'json',
rootProperty: 'customers',
totalProperty: 'count'
}
}
});
Ext.define('App.model.Order', {
extend: 'Ext.data.Model',
idProperty: 'orderNumber',
fields: [
{ name: 'orderNumber', type: 'int' },
{
name: 'customerNumber', reference: {
type: 'App.model.Customer',
inverse: 'orders'
}
},
{ name: 'orderDate', type: 'string' },
{ name: 'status', type: 'string' }
],
proxy: { // Note that proxy is defined in the Model, not the Store
type: 'ajax',
url: '../../../api/OrdersAssociationsReference',
reader: {
type: 'json',
rootProperty: 'orders',
totalProperty: 'count'
}
}
});
Ext.application({
name: 'App',
models: ['Customer', 'Order'],
stores: ['Customers', 'Orders'],
launch: function () {
var customersStore = Ext.getStore('Customers');
customersStore.load(function (records, operation, success) {
var customer = records[0],
orders = customer.orders(),
order;
orders.load(function (records, operation, success) {
console.log('Orders for ' + customer.get('customerName') + ':\n-------------------------------------------------------');
for (i = 0, len = records.length; i < len; i++) {
order = records[i];
console.log('orderNumber: ' + order.get('orderNumber'));
console.log('orderDate: ' + order.get('orderDate'));
console.log('status: ' + order.get('status'));
console.log('-------------------------------');
}
})
});
}
});

ExtJS TreeStore is empty if I load store by manually

Model
Ext.define('MyDesktop.model.mail.MailFoldersModel', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.field.String'
],
fields: [
{
type: 'string',
name: 'id'
},
{
type: 'string',
name: 'idParent'
},
{
type: 'string',
name: 'text'
}
]
});
My TreeStore
Ext.define('MyDesktop.store.mail.MailFoldersStore', {
extend: 'Ext.data.TreeStore',
requires: [
'MyDesktop.model.mail.MailFoldersModel'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'MailFoldersStore',
model: 'MyDesktop.model.mail.MailFoldersModel',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'http://url/mail/folders',
reader: {
type: 'json',
rootProperty: 'items',
successProperty: 'success'
}
},
root: {
text: 'root',
iconCls: 'mail-folders-owner'
}
}, cfg)]);
}
});
Store is autoloaded, all works correctly, store contains 11 records.
var MailFoldersStore = Ext.create('MyDesktop.store.mail.MailFoldersStore', {
storeId: 'MailFoldersStore'
});
If I set autoLoad to false and trying to load by manually - store is empty, 0 records.
var MailFoldersStore = Ext.create('MyDesktop.store.mail.MailFoldersStore', {
storeId: 'MailFoldersStore'
});
MailFoldersStore.load({
callback : function(records, operation, success) {
console.log(records);
}
});
What can be a reason for this behaviour?
I also has same problem. I am using Extjs 5.1. After googling I found one complex solution which needs us to modify the framework.
See the below link if it can help you.
http://www.sencha.com/forum/showthread.php?154823-listeners-quot-exception-quot-in-proxy.ajax-on-TreeStore-do-not-work

Is Sencha ExtJS association POST wrong?

So, I have a problem using Sencha ExtJs 4.1 Associations.
I have something like:
Models
Ext.define('Tpl.model.User', {
extend: 'Ext.data.Model',
requires: ['Tpl.model.PostTemplate'],
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' },
{ name: 'postTemplate', type: 'int' }
],
associations: [
{ type: 'belongsTo', name: 'postTemplate', model:'Tpl.model.PostTemplate', associationKey: 'postTemplate', primaryKey: 'id', foreignKey: 'postTemplate' }
]
});
and
Ext.define('Tpl.model.PostTemplate', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'blah', type: 'string' }
],
associations: [
{ type: 'hasMany', model: 'Tpl.model.User' }
]
});
Stores
Ext.define('Tpl.store.Users', {
extend: 'Ext.data.Store',
model: 'Tpl.model.User',
autoLoad: true,
proxy: {
type: 'rest',
url: '../users',
reader: {
type: 'json',
root: ''
},
writer: {
type: 'json'
}
}
});
Ext.define('Tpl.store.PostTemplate', {
extend: 'Ext.data.Store',
model: 'Tpl.model.PostTemplate',
autoLoad: true,
proxy: {
type: 'rest',
//appendId: true,
url: '../postTemplates/',
reader: {
type: 'json',
root: ''
},
writer: {
type: 'json'
}
}
});
The problem is that I'm getting a POST like this:
{
"postTemplate": 1,
"id": 0,
"name": "foo"
}
But I need a POST like this:
{
"postTemplate": {
"id": 1,
"blah": "foo"
},
"id": 0,
"name": "bar"
}
Also, the assessor function like "setPostTemplate" doesn't exist and I think it should be created automatically.
Already tried to something like " record.data.postTemplate = (...) " but I got an infinite loop throwing an execption...
Can someone please advise? Also, if you need something else that could be useful for the answer let me know.
Thanks!
Out of the box the Associated objects are not serialized back to the server as you expect them . This issue has been brought up many times. The best thing might be to follow this thread:
http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store
This thread talks about overriding getRecordData in the JSON writer class.

Extjs 4, Dynamic Tree and store re-mapping

I need to Extjs Tree panel with dynamic remote data(JSON) for file listing.
and the date field name is not fit to Extjs tree store field. so I need to re-mapping to make fit, like adding leaf field and text field.
the return JSON data is like this:
[{
"id":1,
"yourRefNo":"A91273",
"documentName":"Test Document",
"documentFileName":"login_to_your_account-BLUE.jpg",
"updatedBy":"root root",
"updatedAt":"\/Date(1343012244000)\/"
}]
and this is the tree panel:
Ext.define('App.view.Document.DocumentList', {
extend :'Ext.tree.Panel',
rootVisible : false,
alias: 'widget.Document_list',
store: 'DocumentList_store'
});
and this is the store:
Ext.define('App.store.DocumentList_store', {
extend: "Ext.data.TreeStore",
model: 'App.model.DocumentList_model',
proxy: {
type: 'ajax',
url: '/Document/GetDocumentList/',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: '' // there is no root
},
pageParam: undefined,
startParam: undefined,
pageParam: undefined
},
root: {
children: []
},
autoLoad: false,
listeners: {
append: function (thisNode, newChildNode, index, eOpts) {
console.log(newChildNode.get('documentName')); // 'Test Document'
newChildNode.set('leaf', true);
newChildNode.set('text', newChildNode.get('documentName'));
// it does not add to tree panel.
}
}
});
after load data from server, and it call the append function well. but after that, nothing show up in tree panel.
What I am doing wrong? please advice me.
Thanks
[EDIT]
This is the model,
Ext.define("App.model.DocumentList_model", {
extend: "Ext.data.Model",
fields: [
'id','yourRefNo','documentName','documentFileName','updatedBy','updatedAt'
]
});
I'm fusing your code with a piece of working code of mine. Try see if this works:
Model:
Ext.define("App.model.DocumentList_model", {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'yourRefNo'},
{name: 'documentName' },
{name: 'documentFileName'},
{name: 'updatedBy'},
{name: 'updatedAt', convert: function(v) { return v;} }, // Notice you can do field conversion here
{name: 'leaf', type: 'boolean', defaultValue: false, persist: false},
],
proxy: {
type: 'ajax',
url: '/Document/GetDocumentList/',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: 'children'
},
},
});
Store:
Ext.define('App.store.DocumentList_store', {
extend: "Ext.data.TreeStore",
model: 'App.model.DocumentList_model',
root: {
text: 'Root',
id: null,
expanded: true
},
autoLoad: false,
});
JSON Response:
{
"success":true,
"children":[{
"id":1,
"yourRefNo":"A91273",
"documentName":"Test Document",
"documentFileName":"login_to_your_account-BLUE.jpg",
"updatedBy":"root root",
"updatedAt":"\/Date(1343012244000)\/",
"leaf":false
}]
}

Resources