views:

386

answers:

1

I have a json property that might be null, and I'd prefer to keep it that way for other reasons. When I include that property in an Ext.Template with '{myProp}' it sometimes renders as the word "null" when I want it to render as an empty string.

{myProp:undef} only hunts for undefined, not null. What's the best way to get this done given that I don't want to modify my data to convert null to empty string, and I'd like, if possible, to keep this inside the template. Shoving a little javascript into the template would be okay, but I would just still like to be able to do myTemplate.apply(myData)

nl2br almost does it, but I need to not insert br tags in the event of newlines.

+1  A: 

Can you just override undef?

Ext.util.Format.undef = function(v){
    return v !== undefined && v !== null ? v : "";
};

Or you could add your own function if you don't want to mess with Ext and bind that in the template instead.

Ext.util.Format.null2str = function(v){
    return v !== null ? v : "";
};
bmoeskau
That was great. I went one further and had null2str invoke undef: Ext.util.Format.null2str = function(v){ return v !== null ? Ext.util.Format.undef(v) : ""; };
Brian Deacon