Sencha Touch: reference not generated - extjs

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

Related

Using multiple model associations with bindings

I am using ExtJS 6.x with three models:
Ext.define('Admin.model.Patient', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.proxy.Rest',
'Ext.data.schema.Association'
],
fields: [
{ name: 'id', type: 'string' },
{ name: 'mrn', type: 'string' },
{ name: 'birth_date', type: 'date', format: 'Y-m-d' },
{ name: 'sex', type: 'string' },
{ name: 'first_name', type: 'string' },
{ name: 'last_name', type: 'string' },
],
style: {
'font-size': '22px',
'color':'red'
},
proxy: {
type: 'ajax',
url: remote.url.patient,
reader: {
type: 'json',
rootProperty: ''
}
}
});
Ext.define('Admin.model.LabResultGroup', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
{ name: 'test_code', type: 'string' },
{ name: 'test_name', type: 'string' },
{
name: 'patient_id',
type: 'string',
reference: {
parent: 'Patient',
inverse: 'lab_results',
autoLoad: false
}
}
],
proxy: {
type: 'ajax',
url: remote.url.labsgroup,
reader: {
type: 'json',
rootProperty: ''
}
});
and
Ext.define('Admin.model.LabResult', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
{ name: 'test_code', type: 'string' },
{ name: 'test_name', type: 'string' },
{ name: 'test_code_system', type: 'string' },
{ name: 'result_value', type: 'string' },
{ name: 'result_value_num', type: 'string' },
{ name: 'collection_datetime', type: 'date', format: 'Y-m-d' },
{ name: 'result_datetime', type: 'date', format: 'Y-m-d' },
{
name: 'lab_id',
type: 'string',
reference: {
parent: 'LabResultGroup',
inverse: 'lab_group',
autoLoad: false
}
}
],
proxy: {
type: 'ajax',
url: remote.url.labs,
reader: {
type: 'json',
rootProperty: ''
}
}
});
I can access the association between LabResultGroup and Patient just fine (between two comboboxes using bindings), but when I try accessing the association between LabResult and LabResultGroup, it does not register.
I will post a Fiddle in due course to exhibit the behavior I am encountering. Is there anything that would prevent associations across models like this?
It actually worked! For some odd reason the parent name in the association on my LabResult model got mucked up. Corrected that, and my association bindings are firing as expected.
(Also, there was a minor problem with the API endpoint I was accessing. I had to add another condition for the property on which it was filtering. Details, shmetails)
Moral of the story: Names are important! Happy Friday peeps! Ha! Ha!

Extjs get Model by Controller

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

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

sencha touch 2: list population with associations in model

as a learning project for sencha-touch2 i'm trying to populate a store with data from https://www.hnsearch.com/api
i have created the model and store as follow and in the inspector view i can see that data is received and correctly mapped.
the problem i cannot figure out is, how to show a sencha xtype:list element with the actual result items nested in the json (Model: SearchResultItem). i tried the following, which will give me a list with ONE list item with the results in it, but i would like to have a list item for each search result.
models:
Ext.define('senchaHackerNews.model.Search', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'hits',
type: 'int'
}],
associations: {
type: 'hasMany',
name: 'results',
model: 'senchaHackerNews.model.SearchResults',
associationKey: 'results'
}
}
});
Ext.define('senchaHackerNews.model.SearchResults', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'score',
type: 'float'
}],
associations: {
type: 'hasOne',
name: 'item',
model: 'senchaHackerNews.model.SearchResultItem',
associationKey: 'item'
}
}
});
Ext.define('senchaHackerNews.model.SearchResultItem', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'username',
type: 'string'
}, {
name: 'title',
type: 'string'
}, {
name: 'points',
type: 'int'
}, {
name: 'url',
type: 'string'
}, {
name: 'domain',
type: 'string'
}]
}
});
store:
Ext.define('senchaHackerNews.store.Search', {
extend: 'Ext.data.Store',
requires: ['senchaHackerNews.model.Search'],
config: {
storeId: 'hnSearchStore',
model: 'senchaHackerNews.model.Search',
autoload: false,
proxy: {
type: 'jsonp',
// url: 'http://api.thriftdb.com/api.hnsearch.com/items/_search?q=ipad',
reader: {
type: 'json',
rootProperty: ''
},
callbackKey: 'callback'
}
}
});
view:
Ext.define('senchaHackerNews.view.Search', {
extend: 'Ext.navigation.View',
alias: 'widget.hnSearch',
xtype: 'hnSearch',
requires: ['Ext.field.Search'],
initialize: function() {
var xtpl = new Ext.XTemplate('<tpl for="results">{item.username} ---- {item.title} | <br></tpl>');
this.add([
{
xtype: 'container',
title: 'Search HN',
layout: 'vbox',
items: [{
xtype: 'searchfield',
placeHolder: 'Search HN News (at least 3 chars)',
listeners: {
scope: this,
clearicontap: this.onSearchClearIconTap,
keyup: this.onSearchKeyUp
}
}, {
xtype: 'list',
flex: 1,
itemTpl: xtpl,
store: 'hnSearchStore',
emptyText: 'No Matching Items',
}]
}
]);
this.callParent(arguments);
},
onSearchKeyUp: function(field) {
if(field.getValue() != '' && field.getValue().length > 3) {
var store = Ext.StoreMgr.get('hnSearchStore');
store.setProxy({
url: 'http://api.thriftdb.com/api.hnsearch.com/items/_search?q='+field.getValue()
});
store.load();
} else if(field.getValue() == '') {
Ext.StoreMgr.get('hnSearchStore').removeAll();
}
},
onSearchClearIconTap: function() {
Ext.StoreMgr.get('hnSearchStore').removeAll();
}
});
Example JSON is here http://api.thriftdb.com/api.hnsearch.com/items/_search?q=facebook&pretty_print=true
AFAIK if you are looking for array of items to be displayed the you should use items model in store and rootProperty pointing to item so
Ext.define('senchaHackerNews.store.Search', {
extend: 'Ext.data.Store',
requires: ['senchaHackerNews.model.SearchResults'],
config: {
storeId: 'hnSearchStore',
model: 'senchaHackerNews.model.SearchResults',
autoload: false,
proxy: {
type: 'jsonp',
// url: 'http://api.thriftdb.com/api.hnsearch.com/items/_search?q=ipad',
reader: {
type: 'json',
rootProperty: 'results'
},
callbackKey: 'callback'
}
}
});
Note the change in model & rootProperty attribute

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.

Resources