1

我试图避免冗余并调用通用 XTemplate 以根据时间戳格式化 GMT 中的日期和时间。

这不会输出我期望的字符串,而只是在 UI 中输出 [Object object]。

SCB.RMWB.templates = {
    timeStamp: function(stamp) {
        return new Ext.XTemplate(   
            '<span class="time-frame">{[ this.dateRangeMsg(stamp) ]}</span>',   
            {
                dateRangeMsg: function(stamp) {

                    console.log('stamp', stamp);    

                    var dateTime = new Date(),
                        now = Ext.Date.format(dateTime, 'U'),           // gives us seconds since the UNIX Epoch from Ext.Date class
                        offset = Ext.Date.format(dateTime, 'Z'),        // gives us GMT offset from Ext.Date class
                        minutesAgo = (now - 300),                       // 5 minutes ago
                        hourAgo = (now - 3600),                         // one hour ago
                        msg = '';


                    if (stamp >= minutesAgo && stamp <= now) {
                        msg = 'Moments ago';
                    } else if (stamp >= hourAgo && stamp <= now){
                        msg = '1 hour ago';
                    } else {
                        msg = this.formatGMT(stamp, offset).toString();
                    }   

                    return msg;

                },
                formatGMT: function(stamp, offset){

                    var time;

                    // * 1000 gives us date string to format in Ext.Date class
                    if (offset > 0){
                        time = new Date((stamp - offset) * 1000);    
                    } else {
                        time = new Date((stamp + offset) * 1000); 
                    } 

                    return Ext.Date.format(time, 'd-M-y, H:i \\G\\M\\T');

                }                   
            }
        );
    },
    notifications: {
        flyout: function(){
            return new Ext.XTemplate(
                '<tpl for=".">',    
                    '<li id="notification-flyout-{id}">',
                        '<div class="data-details">',
                            '<p>{message}</p>',
                            '{[ this.renderTimeStamp(values.sentDate) ]}',   
                        '</div>',   
                    '</li>',   
                '</tpl>',
                {
                    renderTimeStamp: function(stamp) {
                        return SCB.RMWB.templates.timeStamp(stamp);
                    }               
                }
            );
        }
    }
};

如果我将时间戳功能保留在原始模板中,它可以正常工作,但此功能将在不同模板的多个地方使用,所以我想要更通用的东西,我可以重复使用。

4

1 回答 1

2

Well I have some difficulties to follow you within your code example but it should work for you when you call apply on your template instance.

In detail:

Create the template you want to reuse once within a helper-namespace. To reuse it provide a method that get's that instance along with provided data.

var tpl = new Ext.Template('Name: {name}, Age: {age}');
tpl.apply({name: 'John', age: 25}); // returns a html string

Here a (more detailed) JSFiddle

于 2012-12-20T13:54:58.957 回答