简体   繁体   English

Sencha Touch Model中的字段转换功能失败

[英]Field conversion function fails in Sencha Touch Model

Im making a Sencha Touch PhoneGap-app. 我正在制作一个Sencha Touch PhoneGap应用程序。 I'm trying to add a field with a conversion function to a model in Sencha Touch 2.1 This conversion function is supposed to find individual letters and replace them ("convFAF"). 我正在尝试将具有转换功能的字段添加到Sencha Touch 2.1中的模型中。该转换功能应该查找单个字母并将其替换(“ convFAF”)。

I can't understand why it fails. 我不明白为什么会失败。

With the function in place, the list that (lists the store that) uses the model shows up blank. 使用该功能后,使用该模型的列表(列出该商店)将显示为空白。 Xcode doesn't print any error message to the console. Xcode不会在控制台上显示任何错误消息。

My model: 我的模特:

Ext.define('Labblistan.model.LabItem', {
extend: 'Ext.data.Model',
config: {
    fields: [
    {name: 'Group', type: 'String'},
    {name: 'Groupname', type: 'String'}, 
    {name: 'Analysis', type: 'String'}, 
    {name: 'Sex', type: 'String'}, 
    {name: 'AgeFrom', type: 'String'}, 
    {name: 'AgeTo', type: 'String'}, 
    {name: 'LowLimit', type: 'String'}, 
    {name: 'UpperLimit', type: 'String'}, 
    {name: 'Unit', type: 'String'},
    {name: 'Comment', type: 'String'},
    {name: 'SortOrder', type: 'String'},
     {
     name : 'formattedAnalysis',
     type : 'string',
     convert : function(v, record) {
     var unformattedAnalysis = record.get('Analysis');
     if (unformattedAnalysis.indexOf("-") != -1) {
     return unformattedAnalysis.substring(unformattedAnalysis.indexOf("-")).substr(1).toUpperCase();
     } else {
     return unformattedAnalysis.toUpperCase();
     }
     }},
     {name: 'formattedAgeFrom',  convert: convertAgeFrom},
    ]
},
});
function convertAgeFrom(v, record) {
    var unformattedAgeFrom = record.get('AgeFrom');
    var formattedAgeFrom = unformattedAgeFrom.replace('d', ' d.').replace('y', ' år').replace('m', ' mån.').replace('w', ' v.');
    return formattedAgeFrom;
}

As you can see I already have a conversion function going ("formattedAnalysis" - which works fine). 如您所见,我已经有了转换功能(“ formattedAnalysis”-可以正常工作)。

I've tried having the function inside the field definition (as per formattedAnalysis) but with the same results. 我尝试过在字段定义内使用函数(按照formattedAnalysis),但结果相同。 I've also tried setting AgeFrom type to Auto (as well auto and string for formattedAgeFrom 我也尝试过将AgeFrom类型设置为Auto(以及auto和formattedAgeFrom的字符串

The strangest part is that if I switch record.get('AgeFrom'); 最奇怪的部分是,如果我切换record.get('AgeFrom'); to 'Analysis', the function works. 到“分析”,该功能有效。 It also seems to works with Group, Unit, and SortOrder but not with AgeFrom or AgeTo (the two fields I'm now interested in converting). 它似乎也适用于Group,Unit和SortOrder,但不适用于AgeFrom或AgeTo(我现在对转换这两个字段感兴趣)。

The data is loaded from an XML file, a copy of which can be found here 数据是从XML文件加载的,可以在此处找到其副本

I thought adding this conversion would be easy but I'm at a total loss here. 我以为添加此转换会很容易,但是我在这里完全不知所措。 Any help or ideas are deeply appreciated. 任何帮助或想法深表感谢。

So, apparently when using Xcode + PhoneGap + Sencha you don't get the same console output that you get in the browser. 因此,显然在使用Xcode + PhoneGap + Sencha时,您不会获得与浏览器相同的控制台输出。 This caused me to miss what was actually causing the problem. 这使我错过了真正导致问题的原因。

Uncaught TypeError: Cannot call method 'replace' of undefined 

so just throwing in a check for the field not being empty solved it: 因此,只需检查字段是否为空即可解决该问题:

function convertAgeFrom(v, record) {
var unformattedAgeFrom = record.get('AgeFrom');
if (unformattedAgeFrom != null) {
var formattedAgeFrom = unformattedAgeFrom.replace('d', ' d.').replace('y', ' år').replace('m', ' mån.').replace('w', ' v.');
return formattedAgeFrom;
}
}

Thanks to ThinkFloyd for requesting the error message. 感谢ThinkFloyd请求错误消息。 Wouldn't have thought to check otherwise. 不会考虑检查其他情况。 (Though my project in its current state cannot run in the browser, I had an earlier prototype which was close enough to throw the same error, does anyone know if there's a way to get more info to the xcode console?) (尽管我的项目目前无法在浏览器中运行,但我有一个较早的原型,该原型足够接近以引发相同的错误,有人知道是否有办法向xcode控制台获取更多信息吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM