eventlisters not working in controller - model-view-controller

I'm working on my very first Sencha Touch 2 project, so I'm not very familiar with it yet. I'm using the Sencha documentation and have been Googling and Stackoverflowing a lot, but can't seem to find the answer to this problem.
I'm working in MVC and want to add some eventlisteners (in the controller) to controls in my view. Whatever I try, they don't seem to work, although they work when I add them in the view itself. Ofcourse that's not best practice at all, so I'm wondering what I'm doing wrong?
This is how my controller looks:
Ext.define("workingTime.controller.MainController", {
extend: "Ext.app.Controller",
views: ['Main'],
refs: [
{
sl_break: '#sl_break'
},
{
sl_work: '#sl_work'
}
],
init: function() {
this.control({
'sl_break': {
change: 'setBreakTime'
}
});
},
setBreakTime: function(newValue) {
console.log('set');
}
});
And this is how my view looks (with the listener still added):
Ext.define("workingTime.view.Main", {
extend: 'Ext.form.Panel',
controllers: ['MainController'],
requires: [
'Ext.field.Slider'
],
config: {
fullscreen: true,
items: [
{
xtype: 'label',
html: '<p class="label_field">Take a <span>five</span> minute break<p>'
},
{
xtype: 'sliderfield',
name: 'sl_break',
value: 5,
minValue: 1,
maxValue: 30,
style: {
'background-color' : '#FFecc0'
},
listeners: {
change: function() {
alert('changed');
}
}
},
{
]
}
});
Tell me if you need more info.

I would try: (without init function)
config: {
refs: {
sl_break: '#sl_break',
sl_work: '#sl_work'
},
control: {
sl_break: {
change: 'setBreakTime'
}
}
},

in your controller add sl_break: 'main sliderfield[itemId=sl_break]' in refs
Ext.define("workingTime.controller.MainController", {
extend: "Ext.app.Controller",
views: ['Main'],
refs: [
{
sl_break: 'main sliderfield[itemId=sl_break]'
}
],
init: function() {
this.control({
'sl_break': {
change: 'setBreakTime'
}
});
},
setBreakTime: function(newValue) {
console.log('set');
}
});
in view add alias to main and itemId to sliderfield
Ext.define("workingTime.view.Main", {
extend: 'Ext.form.Panel',
controllers: ['MainController'],
alias: 'widget.main',
requires: [
'Ext.field.Slider'
],
config: {
fullscreen: true,
items: [
{
xtype: 'label',
html: '<p class="label_field">Take a <span>five</span> minute break<p>'
},
{
xtype: 'sliderfield',
name: 'sl_break',
itemId:'sl_break',
value: 5,
minValue: 1,
maxValue: 30,
style: {
'background-color' : '#FFecc0'
},
listeners: {
change: function() {
alert('changed');
}
}
},
{
]
}
});
i hope it will work

Related

How do I lock a carousel in Sencha Touch

I am using a carousel and would like to lock the carousel until a button is clicked. Is there an easy way to do this? Thanks!
My code so far:
Ext.define('BabyBen.view.MainCarousel', {
extend: 'Ext.carousel.Carousel',
xtype: 'maincarousel',
config: {
fullscreen: true,
activeItem: 1,
indicator: false,
scrollable: {
direction: 'vertical',
directionLock: true
},
items: [{
xtype: 'whatscreen'
}, {
xtype: 'startscreen'
}, {
xtype: 'whenscreen'
}]
}
});
You need to write a custom view for lockable carousel:
Ext.define("myApp.view.LockableCarousel",{
extend: 'Ext.Carousel',
initialize: function () {
this.onDragOrig = this.onDrag;
this.onDrag = function (e) { if(!this.locked){this.onDragOrig(e);} }
},
locked: false,
lock: function () { this.locked = true; },
unlock: function () { this.locked = false; }
});
Then you can extend this custom carousel anywhere using extend as well as you need to apply custom lock and unlock function for your desired lockable carousel through button handler:
Ext.define("myApp.view.CustomCarousel",{
xtype: 'CustomCarousel',
extend: 'myApp.view.LockableCarousel',
config: {
id: 'LockableCarousel',
title: 'Example4',
iconCls: 'cloud',
indicator: false,
items: [
{
html : 'Item 1',
style: 'background-color: #5E99CC'
},
{
items: [
{
xtype : 'button',
text: 'Lock',
handler:function() {
Ext.getCmp('LockableCarousel').lock();
}
},
{
xtype : 'button',
text: 'Unlock',
handler:function() {
Ext.getCmp('LockableCarousel').unlock();
}
}
]
}
]
}
});
Working Demo

SenchaTouch: Ext.getCmp().setSrc() method is not working

I am trying to change the source of an image using setSrc but I get this error:
Uncaught TypeError: Cannot call method 'setSrc' of undefined
Ext.Ajax.request.success Ext.apply.callback Ext.define.onComplete
Ext.define.onStateChange (anonymous function)
This is my source code :
View Page:
Ext.define('MechanicalTerk.view.Picture', {
extend: 'Ext.Container',
xtype: 'Picture',
requires: [
'Ext.data.Store'
],
config: {
layout: 'vbox',
items:
[
{
xtype: 'toolbar',
docked: 'top',
title: 'Requests',
minHeight: '60px',
items: [
{
xtype:'button',
ui:'back button',
id:'backButton',
text:'Back'
},{
xtype: 'button',
id:'logoutButton',
text: 'Logout'
}
],
},
{
xtype:'image',
height:500,
id:'layoutImage'
}
]
}
});
Controller:
Ext.define('MechanicalTerk.controller.PictureController',{
extend:'Ext.app.Controller',
showPicture: function(userID,sentAt){
Ext.Ajax.request({
url:'app/Model/database.php',
params: {
functionID: 3,
userID: userID,
sentAt: sentAt,
},
success: function (response, opts){
Ext.getCmp('layoutImage').setSrc('http://www.sencha.com/img/20110215-feat-perf.png');
Ext.Viewport.setActiveItem(Ext.create('MechanicalTerk.view.Picture'));
}
});
}
});
Any ideas ?
You should consider not using your own id and instead use an itemId. This is probably happening because you are trying to re-use a component that has been destroyed. I would suggest creating a ref to your image in your controller so you can access it.
So I'm thinking:
Ext.define('MechanicalTerk.controller.PictureController',{
extend:'Ext.app.Controller',
config: {
refs : {
myImage : {
autoCreate: true,
selector: '#myimage',//assuming your image's itemId is 'myimage'
xtype: 'image'
}
}
},
showPicture: function(userID,sentAt){
Ext.Ajax.request({
url:'app/Model/database.php',
params: {
functionID: 3,
userID: userID,
sentAt: sentAt,
},
success: function (response, opts){
var me = this;
me.getMyImage().setSrc('http://www.sencha.com/img/20110215-feat-perf.png');
Ext.Viewport.setActiveItem(Ext.create('MechanicalTerk.view.Picture'));
}
});
}
});
PS: Not tested but try that approach.

Sencha Touch 2 DataView itemtaphold not firing

For the life of me I'm unable to get my dataview to fire an itemtaphold event no matter how I attach the event handler. The itemtap event fires just fine. The closest i've been able to come is to use the longpress on the dom elements but that doesn't give me a reference to my control, just the dom element. Is the itemtaphold event broken for dataview on sencha touch 2 commercial?
BrowserGrid.js:
Ext.define('MyApp.view.BrowserGrid', {
extend: 'Ext.dataview.DataView',
xtype: 'browsergrid',
requires: [
'MyApp.view.BrowserGridItem',
'Ext.dataview.*',
'Ext.plugin.*'
],
config: {
useComponents: true,
cls: 'browser-grid',
defaultType: 'browsergriditem',
scrollable:{
direction: 'vertical',
directionLock: true
},
plugins :[{xclass:'Ext.plugin.ListPaging',autoPaging: true}],
listeners:
{
itemtaphold: function() { console.log('tapped'); }
}
}
});
BrowserGridItem.js
Ext.define('MyApp.view.BrowserGridItem', {
extend: 'Ext.dataview.component.DataItem',
xtype : 'browsergriditem',
config: {
cls: 'browser-grid-item',
dataMap: {
getName: {
setHtml: 'FILE_NAME'
},
getImage: {
setSrc: 'IMG_URL'
}
},
name: {
cls: 'x-name'
},
image: {
height:150,
width: 150,
flex:1
},
layout: {
type: 'vbox',
align: 'center'
}
},
applyImage: function(config) {
return Ext.factory(config, Ext.Img, this.getImage());
},
updateImage: function(newImage, oldImage) {
if (newImage) {
this.add(newImage);
}
if (oldImage) {
this.remove(oldImage);
}
},
applyName: function(config) {
return Ext.factory(config, Ext.Component, this.getName());
},
updateName: function(newName, oldName) {
if (newName) {
this.add(newName);
}
if (oldName) {
this.remove(oldName);
}
}
});

Sencha 2: Listeners on a panel not working

I'm trying to port my Sencha app from Sencha1 to Sencha2.
It seems that none of my listeners are working. The documentation for Sencha2 seems to have different events, and a smaller number of events:
http://docs.sencha.com/touch/1-1/#!/api/Ext.Panel
http://docs.sencha.com/touch/2-0/#!/api/Ext.Panel
Is there a new way to do this? Are the listeners from Sencha1 just not implemented in Sencha2 yet?
Ext.define('MyApp.view.Loading', {
extend: 'Ext.Panel',
googleAnalyticsName: 'Loading',
id: 'loadingView',
xtype: 'loading',
config: {
fullscreen: true,
layout: 'vbox',
scrollable: false,
items: [{
html: '<div id="loading-view" style="background-repeat: none;"><div id="loading-page-spinner"></div>'
}],
listeners: {
activate: function() {
console.log('activate listener');
},
afterrender: function() {
console.log('afterrender listener')
}
},
},
});
This seems to work:
Ext.define('MyApp.view.Loading', {
extend: 'Ext.Panel',
googleAnalyticsName: 'Loading',
id: 'loadingView',
xtype: 'loading',
initialize: function() {
this.on('activate', function() { alert('activate'); } );
this.callParent();
}
...

How to add a form xtype sencha touch 2.0

Currently I am teaching myself Sencha Touch 2.0 and I am encountering a problem with adding a xtype called test to my viewport.
The test class is extending "Ext.form.Panel'" the problem is that no form shows up in my view and sencha also doesn't give any errors. When I extend "Ext.Panel" and set a html attribute this does show up. Can someone tell me what i am doing wrong ?
Viewport file
Ext.define('App.view.Viewport', {
extend: 'Ext.viewport.Default',
config: {
scrollable: true,
fullscreen:true,
items:[
{
xtype: "panel",
items: [
{
xtype:"toolbar",
title:"Test App"
},
{
xtype:"panel",
items: [
{
xtype:"test"
}
]
}
]
}
]
}
});
Controller file
//Define controller name
Ext.define('App.controller.User', {
//Extend the controller class
extend: 'Ext.app.Controller',
//define associated views with this controller
views: ['user.Test'],
init: function()
{
//do something and setup listeners
//setup listeners
}
});
View File
Ext.define('App.view.user.Test', {
extend: 'Ext.form.Panel',
alias: 'widget.test',
config: {
items: [
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'passwordfield',
name: 'password',
label: 'Password'
}
]
},
initialize: function() {
console.log('initialize home view');
this.callParent();
}
});
Simply specify
xtype: 'test'
in your App.view.user.Test definition
I have got it sorted. Have a look at:
http://www.sencha.com/forum/showthread.php?191765-My-Ext.form.Panel-object-does-not-want-to-show&p=767689#post767689

Resources