Ext


Go Back   Ext JS Forums > Ext JS Community Forums (2.0) > Ext 2.x: Help

Reply
 
Thread Tools
  #1  
Old 01-04-2008, 03:23 AM
blutarsky blutarsky is offline
Ext User
 
Join Date: Oct 2007
Location: Milan - Italy
Posts: 39
blutarsky is on a distinguished road
Send a message via MSN to blutarsky Send a message via Skype™ to blutarsky
Default XTemplate: check if a property exists

Hi everybody and happy new year,

Is there a way to check if a property used in the XTemplates exists?

For example, if I try this:
var tpl = new Ext.XTemplate(
    '<p>Name: {name}</p>',
    '<p>Kids: ',
    '<tpl for="kids">',
        '<tpl if="age > 1">',
            '<p>{name}</p>',
            '<p>Dad: {parent.name}</p>',
        '</tpl>',
    '</tpl></p>'
);
tpl.overwrite(panel.body, data);
and 'age' property does not exists I get an error.

Thanks a lot.
Reply With Quote
  #2  
Old 02-18-2008, 08:45 AM
efattal's Avatar
efattal efattal is offline
Ext JS Premium Member
 
Join Date: Dec 2007
Posts: 21
efattal is on a distinguished road
Question Unresolved problem

I have the same problem (in a "for" loop)

Does anyone have a solution?

Thanks
Reply With Quote
  #3  
Old 02-18-2008, 09:00 AM
efattal's Avatar
efattal efattal is offline
Ext JS Premium Member
 
Join Date: Dec 2007
Posts: 21
efattal is on a distinguished road
Post One solution may be...

Well, I found a solution, but is it the right one?

If I have an optional property in my objects: for example: the firstname is optional: heroes = [{lastname:"Man", firstname:"Bat"}, {lastname:"Joker"}], I can loop through the array with the following code:

new Ext.XTemplate(
    '<tpl if="this.exists(heroes)">',
    '<fieldset>',
    '<legend>My heroes</legend>',
    '<tpl for="heroes">',
        '{[firstname=""]}',
        '<tpl if="this.exists(firstname)">',
             '{firstname} ',
        '</tpl>',
        '{lastname}',
    '</tpl>',
    '</fieldset>',
    '</tpl>',
    {
        exists: function(o){
        return typeof o != 'undefined' && o != null && o!='';
    }
});
In fact, as XTemplate uses "with" for accessing object properties, if ever the property doesn't exist, JS will use the declared variable that has the same name.

Thanks for your comment

Last edited by efattal; 02-18-2008 at 09:03 AM.. Reason: Precision
Reply With Quote
Reply

Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -5. The time now is 08:57 PM.

© 2006-2009 Ext, LLC
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.