无法在 sencha touch 中的 localstorage 上设置嵌套的 json 数据

Posted

技术标签:

【中文标题】无法在 sencha touch 中的 localstorage 上设置嵌套的 json 数据【英文标题】:Unable setting nested json data on localstorage in sencha touch 【发布时间】:2013-08-22 03:54:52 【问题描述】:

我在 sencha 的 localstorage 上设置嵌套 json 数据时遇到问题。

我有商店类:

 Ext.define('default.store.Top25Store',
extend: 'Ext.data.Store', 
config:

    autoLoad:true,
    model: 'default.model.Top25WordFinal',    
    id:'TodaysWord',
    proxy:
    
        type: 'ajax',
        url: 'http://alucio.com.np/trunk/dev/sillydic/admin/api/word/my_favourite_words_with_definition/SDSILLYTOKEN/650773253e7f157a93c53d47a866204dedc7c363', 
        reader:
        
                type:'json',
                rootProperty:''
        
    

);

和模型类:

Ext.define('default.model.Top25WordFinal', 
    extend: 'Ext.data.Model',

    requires: ['default.model.Top25WordRaw'],

    config: 
        fields: [
            name: 'status', mapping: 'status',
            name: 'message', mapping: 'message',
            name: 'name', mapping: 'name',
            name:'data', mapping: 'data',
            name: 'definition', mapping: 'definitions.definition',
            name: 'ratings', mapping: 'definitions.rating',
            name:'def_id', mapping:'definitions.def_id',
        ],
    
);

和相关的模型类:

Ext.define('default.model.Top25WordRaw', 
    extend: 'Ext.data.Model',
    config: 
        fields: [
        'name',
        'author',
        'word_id',
        'category',
        'definition',
        'rating',
        'def_id',
        'example',
        'author',
        'is_favourite'
        ],
        belongsTo: "default.model.Top25WordFinal"
    
);

可离线使用的机型:

Ext.define('default.model.Top25WordRawOffline', 
    extend: 'Ext.data.Model',

    requires: ['default.model.Top25WordOffline'],

    config: 
        fields: [
            name: 'status', mapping: 'status',
            name: 'message', mapping: 'message',
            name: 'name', mapping: 'name',
            name:'data', mapping: 'data',
            name: 'definition', mapping: 'definitions.definition',
            name: 'ratings', mapping: 'definitions.rating',
            name:'def_id', mapping:'definitions.def_id',
        ],
        identifier:'uuid', // IMPORTANT, needed to avoid console warnings!
    proxy: 
      type: 'localstorage',
      id  : 'Top25Display'
    
    
);

其中 default.model.Top25WordOffline 就像 default.model.Top25WordRaw

显示准确格式的视图:

store: 'Top25Store',
       itemTpl: new Ext.XTemplate(
           '<div>',                
           '<tpl for="data">',
           '<ul class="parabox">',
           '<li><h2 ><b>name</b> </h2>',                          
           '<tpl for="definitions">',
           '<ul class="para-box-wrapper">',
           '<li class="rating"><div id = "definition" class="rating">','definition',
           '<span class="authorBox"><i>Author: author</i></span>',
           '<div id = "favourite" class="is_favourite" ></div>',
           '</li>' ,
        '</div>',
        '</ul></li>', 
        '</tpl>',
        '</ul>',
        '</tpl>',
        '</div>'
        ),

我看到了价值。但是存储在本地存储中的问题:在控制器中,

localStore.getProxy().clear();
         store.each(function(record) 
         var rec = 
         message : record.data.message,
         definition : record.data.definitions.definition,
         ratings:"red"
         // defintion:record.data.definitions.def_id
        ;

        localStore.add(rec);
        localStore.sync();

我已在本地存储和静态评级上设置消息,但定义存在问题。如何存储定义,请清除。

【问题讨论】:

发布 default.model.OnlineFinal 代码并存储您循环通过的是 default.store.News .. 对吗? 请检查我编辑的新店铺,我发错店铺了。 我可以查看 localStore 中使用的模型吗? 使用 Top25WordFinal 模型要存储哪些数据 好吧,我希望你能。 【参考方案1】:

尝试改变

var rec = 
         message : record.data.message,
         definition : record.data.definitions.definition,
         ratings:"red"
         // defintion:record.data.definitions.def_id
        ;

var rec = Ext.create('default.model.Top25WordOffline', 
         
             message : record.data.message,
             definition : record.data.definitions.definition,
             ratings:"red"
         );

【讨论】:

以上是关于无法在 sencha touch 中的 localstorage 上设置嵌套的 json 数据的主要内容,如果未能解决你的问题,请参考以下文章

Sencha Touch cordova.local.properties不见了。

Sencha Touch 选择字段无法显示数据存储中的值

Sencha Touch 2 中的 iPhone 上的 Google Chrome 浏览器无法正常工作

我无法将数据从控制器传递到 Sencha Touch 2.4 中的视图

无法在 Sencha-Touch2 中使用 Ext.Ajax 运行 javascript?

Sencha-Touch:未捕获的类型错误:无法读取未定义的属性“代理”