Sench Touch 2 - Rendering a List - extjs

I have a simple MVC Sencha Touch application, with 1 store, 2 models and 2 views - a toolbar and a list. My toolbar renders fine, but the list does not. No exception is thrown and I can't find what I'm doing wrong.
The store (Books.js):
Ext.define('App.store.Books', {
extend: 'Ext.data.Store',
model: 'App.model.Book',
autoLoad: true,
data: [
{ id: '1', name: '1984', publisher: 'Orwell' },
{ id: '2', name: 'Biography', publisher: 'abcde' },
{ id: '3', name: 'The Old Man and the Sea', publisher: 'Hemingway' }
]
});
The view (List.js - I have another Bar.js which renders fine):
Ext.define('App.view.List', {
extend: 'Ext.List',
store : 'Books',
xtype : 'mylist',
itemTpl: '<div><strong>Name: {name}</strong>Publisher: {publisher}</div>'
});
The viewport (Viewport.js) - extends Ext.Container as I saw in several examples:
Ext.define('App.view.Viewport', {
extend: 'Ext.Container',
requires : [
'App.view.Bar',
'App.view.List'
],
config: {
fullscreen: true,
layout: 'fit',
items: [
{
xtype : 'toolbar',
docked: 'top'
},
{
xtype: 'mylist'
}
]
}
});
As I wrote - my toolbar is shown, my list ('mylist') isn't.
What am I missing or doing wrong?
Thanks

Try adding a config to your view
Ext.define('App.view.List', {
extend: 'Ext.List',
config: {
title: 'Books',
cls: 'books',
store: 'Books',
itemTpl: '<div><strong>Name: {name}</strong>Publisher: {publisher}</div>'
}
});

Related

Sencha touch Error: Uncaught Error The following classes are not declared even if their files have been loaded: 'Ext.data.model'

"Uncaught Error: The following classes are not declared even if their files have been loaded: 'Ext.data.model'. Please check the source code of their corresponding files for possible typos: 'touch/src/data/model.js "
I am getting this error which I have no idea why is being generated. Here is my code have a look because i have turned every stone But still no answer. It seems every tiem i write a code few hours goes to figuring out what is wrong with sencha touch
App.js
//<debug>
Ext.Loader.setPath({
'Ext': 'touch/src',
'TutsPlus': 'app'
});
//</debug>
Ext.application({
//http://docs.sencha.com/touch/2-1/#!/api/Ext.app.Application
name: 'TP',
views: ['Main'],
models: ['mtask'],
stores: ['stask'],
launch: function() {
// Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();
// Initialize the main view
Ext.Viewport.add(Ext.create('TP.view.Main'));
}
});
In Store stask.js
Ext.define('TP.store.stask', {
extend: 'Ext.data.Store',
config: {
//Define which model we are going to use for our store
model: 'TP.model.mtask',
data: [
{label: 'Make the change'},
{label: 'Take the trash'},
{label: 'Clear the room'},
{label: 'Wake Up Early'}
]
}
});
In Model mtask.js
Ext.define('TP.model.mtask', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'label', type: 'string'},
{nae: 'done', type: 'boolean', defaultValue: false}
]
}
});
In view folder Main,js
Ext.define('TP.view.Main', {
extend: 'Ext.Panel',
xtype: 'main',
requires: [
'Ext.TitleBar',
'Ext.dataview.List'
],
config: {
layout: 'vbox',
items: [
{
docked: 'top',
xtpe: 'titlebar',
title: 'Note Taker',
items: [
{
iconCls: 'add',
iconMask: true,
align: 'right',
id: 'add-button'
}
]
},
{
xtype: 'list',
store: 'stask'
}
]
}
});
For some reason, your model class is not getting declared. Try finding out the reason with syntax errors and cases.
Try changing:
nae: 'done' to name: 'done'

Sencha Touch Combo Box using Model and Store

I have this on model
Ext.define('MyApp.model.job', {
extend: 'Ext.data.Model',
alias: 'model.job',
config: {
fields: [{
name: 'name',
type: 'string'
}, {
name: 'value',
type: 'string'
}]
}
});
And this on store
Ext.define('MyApp.store.job', {
extend: 'Ext.data.Store',
alias: 'store.job',
requires: [
'MyApp.model.job'
],
config: {
autoLoad: true,
data: [
{
name: '--Please Select--',
value: ''
}, {
name: 'Job 1',
value: 'Job1'
}, {
name: 'Job 2',
value: 'Job2'
}, {
name: 'Job 3',
value: 'Job3'
}
],
model: 'MyApp.model.job',
storeId: 'jobStore'
}
});
And this is the View
{
xtype : 'selectfield',
name : 'job',
label : 'Job',
displayField : 'name',
valueField : 'value',
store: 'jobStore'
}
But I got the following error on Console
"Uncaught TypeError: Cannot call method 'on' of undefined"
Can someone help me?
In the job store, remove the unneccessary line storeId: 'jobStore' and in the view edit store to store: 'job' and your code will be working fine.
Beside this, i would ask you to remove few line of code from your snippet(Because in standard Sencha coding, we follow certain rules).
requires: [
'MyApp.model.job'
],
alias: 'store.job'
alias: 'model.job'
Happy coding. :)
EDIT
In app.js of your application, you should write
models: ['job'],
stores: ['job']
instead model and store.

Load data to form in mvc

In my appliacation , I have a list and detail(form).I want to load data to Detail view(set data to textfields of form) when list item is clicked. For both list and detail, I am getting data from remote server. I am following MVC.
Now, When listItem is clicked, I am able to get data from server and save it to store and also showing detail view. But I am not able to bind data from store to textfields in form.
Model
Ext.define('App.model.Details', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'Name', type: 'string'},
{name: 'BillingStreet', type: 'string'},
{name: 'BillingCity', type: 'string'}
]
}
});
Store
Ext.define('App.store.Details', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.Details',
autoLoad :true,
grouper : function(record) {
return record.get('Name')[0];
},
}
});
list view
Ext.define('App.view.View', {
extend: 'Ext.List',
alias:'widget.contactlist',
fullscreen: true,
id: 'contactlist',
config:{
disableSelection:true,
store:'Contacts',
itemTpl:'{Name}',
items:[
{
xtype:'toolbar',
docked:'top',
title:'Leeds List'
}
]
}
});
Detail view
Ext.define("App.view.ListDetail", {
extend: "Ext.form.Panel",
requires: "Ext.form.FieldSet",
alias: "widget.listDetail",
config:{
scrollable:'vertical'
},
initialize: function () {
this.callParent(arguments);
var topToolbar = {
xtype: "toolbar",
docked: "top",
title: "Details"
};
this.add([
topToolbar,
{ xtype: "fieldset",
items: [{
xtype: 'textfield',
store: 'Details',
value : 'Name',
label: 'Name'
},
{
xtype: 'emailfield',
store: 'Details',
value : 'BillingStreet',
label: 'Email'
},
{
xtype: 'passwordfield',
store: 'Details',
value : 'BillingCity',
label: 'Password'
}
]
}
]);
}
});
Controller
Ext.define('App.controller.Main', {
extend: 'Ext.app.Controller',
config: {
refs: {
// We're going to lookup our views by xtype.
contactlist: "contactlist",
contactdetail: "listDetail",
//f_name:"#f_name"
},
control: {
contactlist: {
// The commands fired by the notes list container.
itemtap: "oneditLeadCommand"
}
},
routes: {
'contactlist': 'activateList'
}
},
activateList: function ()
{
Ext.Viewport.animateActiveItem(this.getContactlist(), this.slideRightTransition);
},
slideLeftTransition: { type: 'slide', direction: 'left' },
slideRightTransition: { type: 'slide', direction: 'right' },
oneditLeadCommand: function (list, index, target, record, e, eOpts)
{
console.log("onEditLead"+record.data.Id);
this.activateLeadDetail(record);
},
activateLeadDetail: function (record)
{
var contactDetail = this.getContactdetail();
//console.log("activateLeadDetail"+contactDetail.textfield);
//contactDetail.setRecord(record); // load() is deprecated.
//this.getF_name().setDisplayField("");
store = Ext.StoreMgr.get('Details');
//console.log("activateLeadDetail"+store);
store.setProxy({
type: 'ajax',
url : 'http://10.0.2.2:8080/SalesForce/leads/get-lead-details/00D90000000jvoU!AR4AQB6Xcjz4UNBKf12WOcYHWc31QxK2.fXTcbCvOq.oBosCrjBezhqm8Nqc1hrf8MKK5LjLAu8ZC5IqB1kdpWvJGLdWd5pJ/'+record.data.Id, // the json file that holds all our contact info.
reader: {
type: 'json'
}
});
store.load();
var record1 = Ext.StoreMgr.get('Details').getAt(0);
console.log("activateLeadDetail"+record1);
Ext.StoreMgr.get('Details').each(function(test){
console.log("for loop"+test.data);
});
contactDetail.setRecord(record1);
Ext.Viewport.animateActiveItem(contactDetail, this.slideLeftTransition);
},
// Base Class functions.
launch: function () {
this.callParent(arguments);
console.log("launch");
},
init: function () {
this.callParent(arguments);
console.log("init");
}
})
Please help to bind data to detail view.
So I'm guessing your Contacts Store is defined somewhere else but since this one is working you didn't paste the code here.
So one quick note on the model, where you should always define an idProperty. This is what Sencha use internally to define the "primary key" on your store and therefore work properly when you reload/refresh your store.
Ext.define('App.model.Details', {
extend: 'Ext.data.Model',
config: {
idProperty: 'Name', // Or something from your server maybe?
fields: [
{name: 'Name', type: 'string'},
{name: 'BillingStreet', type: 'string'},
{name: 'BillingCity', type: 'string'}
]
}
});
Secondly, why did you use the initialize method in your ListDetail view when you used the config method in your listView? if you specify the config instead, you will be able to to reuse some of this component in a more easy way somewhere else by doing something like
items: [
{
xtype: 'ListDetail',
anyOtherAttribute: value
}
]
But that's kinda out of scope here. But anyway. So what is wrong here I think is that you have defined a Store for each field of your panel. I'm sorry i can't test my hypothesis, but here is what I would do:
Ext.define("App.view.ListDetail", {
extend: "Ext.form.Panel",
requires: "Ext.form.FieldSet",
alias: "widget.listDetail",
config:{
scrollable:'vertical'
items:[
{
xtype: "toolbar",
docked: "top",
title: "Details"
},
{
xtype: "fieldset",
itemId: 'detailedListFiledset', //Overall, prefer itemId
items: [
{
xtype: 'textfield',
value : 'Name',
label: 'Name' // may be you want placeholders here?
},
{
xtype: 'emailfield',
value : 'BillingStreet',
label: 'Email' // and here..
},
{
xtype: 'passwordfield',
value : 'BillingCity',
label: 'Password' // and here..
}
]
}
]
}
});
Alright, and now the issue seems to be in your controller:
Add a ref to your fieldset
Add a reference to your store
create a afterload callback when your store is loaded (Details)
Either clear the store every time or append data and apply to filter to get the correct record (this is why the idProperty is very useful)
set the record of the fieldset and not the panel
I haven't had the chance to try that, but I'll do it later tonight. But git it a go though.
-- EDIT --
Ok I've finally been abe to code something for you.
A few issues were in your code. I don't really know why you need two stores, but let's say you do. I'm going to give you all the files I used (the two stores, the two models, the three views and the controller). Three main thing were wrong in your code:
you should not load the second store and try to get the record right after. use and 'load' or 'refresh' event for that
SetValues for a form is the correct function to use
You were missing the name property in your form so that the form know to which value of the store/model to bind to the field.
ContactModel:
Ext.define('App.model.Contact', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
}
});
ContactStore:
Ext.define('App.store.ContactStore', {
extend: 'Ext.data.Store',
requires:['App.model.Contact'],
config: {
storeId: 'ContactStore',
model: 'App.model.Contact',
autoLoad :true,
data: [
{id: 0, name: 'Foo'},
{id: 1, name: 'Bar'}
]
}
});
DetailModel:
Ext.define('App.model.Detail', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'billingStreet', type: 'string'},
{name: 'billingCity', type: 'string'}
]
}
});
DetailStore:
Ext.define('App.store.DetailStore', {
extend: 'Ext.data.Store',
config: {
model: 'App.model.Detail',
autoLoad :true,
data: [
{id: 0, name: 'Foo', billingStreet:'here', billingCity: 'Somewhere'},
{id: 1, name: 'Bar', billingStreet:'there', billingCity: 'Somewhere else'}
]
}
});
ContactView:
Ext.define('App.view.ContactList', {
extend: 'Ext.List',
xtype: 'contactList',
fullscreen: true,
config: {
itemId: 'contactList',
store:'ContactStore',
emptyText: 'test',
itemTpl: new Ext.XTemplate(
'{name}'
),
items:[
{
xtype:'toolbar',
docked:'top',
title:'Leeds List'
}
]
}
});
DetailView:
Ext.define('App.view.Detail', {
extend: 'Ext.form.Panel',
requires: ['Ext.form.FieldSet'],
xtype: "detail",
config:{
scrollable:'vertical',
items: [
{
xtype: 'toolbar',
docked: 'top',
title: 'Details'
},
{
xtype: 'fieldset',
itemId: 'detailForm',
items: [{
xtype: 'textfield',
store: 'Details',
name: 'name',
placeHolder : 'Name',
label: 'Name'
},
{
xtype: 'textfield',
store: 'Details',
placeHolder : 'BillingStreet',
name: 'billingStreet',
label: 'BillingStreet'
},
{
xtype: 'textfield',
store: 'Details',
placeHolder : 'BillingCity',
name: 'billingCity',
label: 'BillingCity'
}
]
}
]
}
});
Main view:
Ext.define('App.view.Main', {
extend: 'Ext.Container',
xtype: 'main',
config: {
layout: 'hbox',
items: [
{
xtype: 'contactList',
flex:1
},
{
xtype: 'detail',
flex:2.5
}
]
}
});
Main Controller:
Ext.define('App.controller.Main', {
extend : 'Ext.app.Controller',
requires: [
'Ext.Toolbar',
'Ext.List',
'App.store.ContactStore',
'App.store.DetailStore',
'App.view.Detail',
'App.view.ContactList'
],
config: {
//#private
detailStore: null,
currentListIndex: -1,
views : [
'App.view.ContactList',
'App.view.Detail'
],
refs: {
list: 'contactList',
detail: 'detail',
detailForm: 'detail #detailForm'
},
control: {
list: {
itemtap: 'handleItemTapList'
}
}
},
launch: function() {
var store = Ext.getStore('DetailStore');
store.on('refresh', 'handleDetailStoreLoad', this);
this.setDetailStore(store);
},
handleItemTapList: function(list, index, target, record) {
this.setCurrentListIndex(index);
this.getDetailStore().load();
},
handleDetailStoreLoad: function (store) {
debugger;
var record = store.getAt(this.getCurrentListIndex());
this.getDetail().setValues(record.data);
}
});
We could argue on a few things but I tried to go straight to the point and make it work. If you have more questions please ask but this example is working for me. In my opinion, you might not need the second store, as the contact detail could be nested in the COntact store, and you could use the hasMany property of the store.
use form.setRecord() to load a record into a form
make sure your form elements have name property values that match the model name properties
as you have a store, you will have to get a reference to ONE model to load, using say getAt() or find() methods to get or find a model in your store

Ext.List messes up rows and display after going forward on a view?

I have a navigation view that contains a list view with a list of items, and when you click the item it pushes a new list detail page. Everything works fine and dandy, but when I go back to the previous list, it shows up all discombobulated.
I've tested this in Webkit and Mobile webkit browsers and the result is the same. I'm looking for any possible feedback or suggestions that might point me towards a) fixing the problem or b) repainting the window when this happens so it corrects itself.
Example:
Navigation view:
Ext.define('CA.view.speaker.Card', {
extend: 'Ext.NavigationView',
xtype: 'xSpeakerCard',
config: {
title: "Speakers",
iconCls: "team",
autoDestroy: false,
items: [
{ xclass: 'CA.view.speaker.List' }
]
}
});
List view:
Ext.define('CA.view.speaker.List', {
extend: 'Ext.List',
xtype: 'xSpeakerList',
config: {
title: "Speaker List",
store: "Speakers",
fullscreen: true,
grouped: true,
indexBar: true,
itemTpl: [
'{first_name} {last_name}'
]
}
});
Detail View being pushed:
Ext.define('CA.view.speaker.Detail', {
extend: 'Ext.Container',
xtype: 'xSpeakerDetail',
config: {
layout: 'vbox',
scrollable: 'vertical',
title: '',
items: [
{
xtype: 'xSpeakerDetailInfo',
docked: 'top'
},
{
xtype: 'list',
store: 'SpeakerSessions',
id: 'speaker-detail-info-session-list',
flex: 3,
itemTpl: [
'{title}'
],
items: [
{
xtype: 'listitemheader',
cls: 'dark',
html: 'Sessions'
}
]
}
]
}
});

Get an Ext.dataview.List instance to show up inside of an Ext.tab.Panel instance

I cannot figure out how to show my Ext.dataview.List object inside of another object. If I try to show the Ext.dataview.List object in the viewport, by itself, I can see my data. But if I try to include it as a component inside of another component, it thinks there is no data (judging from the HTML output), and doesn't appear to display anything.
So, if I have an app.js with the following code:
Ext.application({
name: 'MobileUnion',
views: ['LocalsList'],
models: ['Local'],
stores: ['Locals'],
launch: function() {
// Initialize the main view
var mainView = {
xtype: 'localslist'
};
Ext.Viewport.add([mainView]);
}
});
I can set up MobileUnion.view.LocalsList this way, which results in no list:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.Container',
alias: 'widget.localslist',
requires: 'Ext.dataview.List',
config: {
items: [
{
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
]
}
});
Or, I can set it up this way, which results in a populated list, but not inside of another component:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.dataview.List',
alias: 'widget.localslist',
config: {
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
});
The reason I am trying to pull this off is so that I can get a list component to display inside of a tab panel.
Note: I have assembled a Fiddle with everything, here: http://www.senchafiddle.com/#6nCH8
I must be missing something basic, but at this point, having gone through the documentation, and purchased two books, one of which I've read fully, and the other of which I am reading while coding, I am still without an answer. Any help would be most greatly appreciated.
Additional Code
app/model/Local.js
Ext.define('MobileUnion.model.Local', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{ name: 'id', type: 'int' },
{ name: 'dateCreated', type: 'date', dateFormat: 'c' },
{ name: 'localName', type: 'string' },
{ name: 'designation', type: 'string' }
],
}
});
app/store/Locals.js
Ext.define('MobileUnion.store.Locals', {
extend: 'Ext.data.Store',
requires: ['MobileUnion.model.Local'],
config: {
model: 'MobileUnion.model.Local',
data: [
{ localName: 'ABCD', designation: '123' },
{ localName: 'WXYZ', designation: '456' }
],
autoLoad: true,
sorters: [{ property: 'dateCreated', direction: 'DESC'}]
}
});
In option 1, you define a container but you actually don't add items to you container. To add your list to the container, add xtype: 'list' to your items array. Next thing you have to do is to set the containers layout to fit, otherwise the list won't show up. This should work for you:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.Container',
alias: 'widget.localslist',
requires: 'Ext.dataview.List',
config: {
layout: {
type: 'fit' //set containers layout
},
items: [
{
xtype: 'list', //add xtype
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
]
}
});
More about layouts in Sencha: http://docs.sencha.com/touch/2-1/#!/guide/layouts
Good luck!

Resources