PDA

View Full Version : [FIXED] Change Template in List? (v2.1)



Stx.Greg
6 Nov 2012, 12:52 PM
I'm trying to extend the new List component in Sencha Touch 2.1 (I'm using RC2) and I can't seem to change the item template dynamically. I'd like to be able to change item templates by pressing a button, but it won't work. It used to work in version 2.0.1. Is there anything I'm missing?

Calling setItemTpl() works in the initialize method, but not in edit() or editCompleted() methods.

Here is the test code I have using.



Ext.application({
name: 'SenchaTest',
launch: function () {


var app = this;


Ext.define('TestModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'field1'},
{name: 'field2'},
{name: 'field3'},
]
}
});


Ext.define('Stx.dataview.MyList', {
extend: 'Ext.dataview.List',


config: {
normalTpl: null,
editTpl: null,
editing: false,
scrollToTopOnRefresh: false,
},


initialize: function () {
this.callParent();
this.setItemTpl(this.getNormalTpl());
},


edit: function () {
if (!this.getEditing()) {
this.setEditing(true);
this.setItemTpl(this.getEditTpl());
}
},


editCompleted: function () {
if (this.getEditing()) {
this.setEditing(false);
this.setItemTpl(this.getNormalTpl());
}
},
});


this.store = Ext.create("Ext.data.Store", {
model: 'TestModel',
data: [
{field1: "data1Field1", field2: "data1Field2", field3: "data1Field3"},
{field1: "data2Field1", field2: "data2Field2", field3: "data2Field3"},
{field1: "data3Field1", field2: "data3Field2", field3: "data3Field3"},
{field1: "data4Field1", field2: "data4Field2", field3: "data4Field3"},
{field1: "data5Field1", field2: "data5Field2", field3: "data5Field3"},
{field1: "data6Field1", field2: "data6Field2", field3: "data6Field3"},
{field1: "data7Field1", field2: "data7Field2", field3: "data7Field3"},
{field1: "data8Field1", field2: "data8Field2", field3: "data8Field3"},
{field1: "data9Field1", field2: "data9Field2", field3: "data9Field3"},
{field1: "data10Field1", field2: "data10Field2", field3: "data10Field3"},
{field1: "data11Field1", field2: "data11Field2", field3: "data11Field3"},
{field1: "data12Field1", field2: "data12Field2", field3: "data12Field3"},
{field1: "data13Field1", field2: "data13Field2", field3: "data13Field3"},
{field1: "data14Field1", field2: "data14Field2", field3: "data14Field3"},
{field1: "data15Field1", field2: "data15Field2", field3: "data15Field3"},
]
});


this.button = Ext.create("Ext.Button", {
text: "Edit List",
listeners: {
tap: function () {
if(!app.list.getEditing()) {
app.list.edit();
} else {
app.list.editCompleted();
}
}
}
});


this.normalTpl = Ext.create("Ext.XTemplate",
'{field1}<br/>',
'{field2}<br/>',
'{field3}',
{});


this.editTpl = Ext.create("Ext.XTemplate",
'{field1}',
{});


this.list = Ext.create("Stx.dataview.MyList", {
store: this.store,
normalTpl: this.normalTpl,
editTpl: this.editTpl,
cls: "mylist",
flex: 1,
});


this.panel = Ext.create('Ext.Panel', {
layout: {type: 'vbox'},
padding: 10,
style: "background-color: #555555;",
items: [
this.button,
{style: "height: 10px;"},
this.list,
],
});


Ext.Viewport.add(this.panel);
}


});

mitchellsimoens
8 Nov 2012, 6:18 AM
Did you try executing refresh method on the list?

vitalyx
8 Nov 2012, 7:14 AM
Hi,

I have the very same issue.

When using setItemTpl on a list from controller, the list shows empty items. Calling list.refresh() makes no effect.
But when assigning itemTpl in the view, the list does render the items.
There was no such behavior in Sencha Touch 2.0.1.

mitchellsimoens
8 Nov 2012, 7:40 AM
I may have just run into this issue myself. Unless any of you have a simple test case then I will have to take the time to create one today some time.

Stx.Greg
8 Nov 2012, 7:41 AM
Did you try executing refresh method on the list?

Yes, and it didn't work. I forgot to include the refresh statement in my example code, but calling refresh() after calling setItemTpl() has no effect.

Stx.Greg
8 Nov 2012, 7:43 AM
I may have just run into this issue myself. Unless any of you have a simple test case then I will have to take the time to create one today some time.

Is my test code not sufficient? It is just a bare-bones app to demonstrate the problem.

mitchellsimoens
8 Nov 2012, 7:46 AM
Is my test code not sufficient? It is just a bare-bones app to demonstrate the problem.

It can be simplified:


var store = new Ext.data.Store({
fields : ['test', 'test2'],
data : [
{
test : 'One',
test2 : 1
},
{
test : 'Two',
test2 : 2
},
{
test : 'Three',
test2 : 3
},
{
test : 'Four',
test2 : 4
},
{
test : 'Five',
test2 : 5
}
]
});

Ext.Viewport.add([
{
xtype : 'toolbar',
docked : 'top',
items : [
{
text : 'Change itemTpl',
handler : function() {
var list = Ext.Viewport.child('list');

list.setItemTpl('{test2}');
}
}
]
},
{
xtype : 'list',
itemTpl : '{test}',
store : store
}
]);

mitchellsimoens
8 Nov 2012, 7:46 AM
I have opened a bug for this.

vitalyx
8 Nov 2012, 8:02 AM
Thanks. Any ETA for Sencha Touch 2.1.1? This is kind of a major bug for any non-trivial real-life project. We are eager to switch to Sencha Touch 2.1 for performance improvements alone, but can't do this because of this bug.

mitchellsimoens
8 Nov 2012, 8:14 AM
There is no public dates but patch releases do come out frequently

vitalyx
8 Nov 2012, 8:18 AM
There is no public dates but patch releases do come out frequently
Available to support subscribers only?

mitchellsimoens
8 Nov 2012, 8:24 AM
Available to support subscribers only?

I'm not part of the decision what releases get to be public or are support subscriber only. Support subscribers do get nightly builds though.

rpsirois
8 Nov 2012, 12:53 PM
The records on screen are being rendered improperly and the ones off the screen are being rendered correctly ( try resizing your browser window and you'll see some that are working ).

alexfluger
9 Nov 2012, 5:48 AM
It seems that list item's tpl property set only when sencha creates this item. And it didn't updated when we call refresh method.

I can suggest two solutions:

1 - Use dataview instead a list (it can be styled to look like a list)
2 - Use this fix:


Ext.override(Ext.dataview.List, {
updateListItem: function(item, index, info) {
var record = info.store.getAt(index);
if (this.isSelected(record)) {
item.addCls(info.selectedCls);
}
else {
item.removeCls(info.selectedCls);
}


item.setTpl(this.getItemTpl());
item.removeCls([info.headerCls, info.footerCls, info.firstCls, info.lastCls]);
this.replaceItemContent(item, index, info)


}
});

rpsirois
9 Nov 2012, 8:55 AM
Thanks for the fix! I hadn't looked at that #setTpl method.

I actually ended up creating new lists for each time I would normally just call #setItemTpl. Horrible, I know, but something had to happen yesterday.

TommyMaintz
12 Nov 2012, 7:59 AM
Try the following override:



Ext.define('ListItemTplFix', {
override: 'Ext.dataview.List',

updateItemTpl: function(newTpl, oldTpl) {
var listItems = this.listItems,
ln = listItems.length || 0,
i, listItem;

for (i = 0; i < ln; i++) {
listItem = listItems[i];
listItem.setTpl(newTpl);
}
this.doRefresh();
}
});

Stx.Greg
12 Nov 2012, 10:32 AM
Thanks Tommy, the fix works great for me.

Alex's workaround was working for me as well.

rpsirois
13 Nov 2012, 9:52 AM
@TommyMaintz thanks nice fix until Sencha figures it out.

qu3s4d4
13 Nov 2012, 12:08 PM
Thanks a lot for that.

joel.m.stein
28 Nov 2012, 4:31 PM
Thanks so much for the fixes above and the bug report!

While migrating an application from Sencha Touch 1.1, I was getting nowhere searching about this problem until I realized it worked in 2.0 but not 2.1.

BostonMerlin
20 Dec 2012, 8:01 AM
Not my intention to throw anyone (Sencha) under the bus but wouldn't regression and unit testing have uncovered this type of bug before it was ever released? If it was a one-off issue, things happen, but i'm seeing more and more bugs introduced after every release that should have been caught. No excuse. The following are just the issues that have effected me since the last update (threads to each of these are on the forums).

- list header groups that are all out of alignment
- list changes requiring many of us to switch over to dataviews.
- odd/even styles on list items now double up
- any app launched from the home screen of an iPhone shows the white-screen-of-death.
- templates that dont refresh
- on and on.

We're not talking about new functionality here, existing functionality now broke. I'm seeing a trend.