【问题标题】:How can I use WebSQL in Sencha Touch with Unique Id如何在 Sencha Touch 中使用具有唯一 ID 的 WebSQL
【发布时间】:2014-09-07 02:27:31
【问题描述】:

这是我的模型:

Ext.define('myApp.model.Category', {
extend: 'Ext.data.Model',

requires: [
    'Ext.data.Field'
],

config: {
    fields: [
        {
            name: 'id',
            type: 'int'
        },
        {
            name: 'slug',
            type:'string'
        },
        {
            name: 'title',
            type:'string'
        },
        {
            name: 'post_count',
            type:'int'
        }
    ],
}});

这是我的商店:

Ext.define('myApp.store.LocalCate',{
extend: 'Ext.data.Store',

requires: [
    'Ext.data.proxy.Sql'
],

config: {
    autoLoad:false,
    model:'myApp.model.Category',
    storeId:'LocalCate',
    proxy:{
        type: 'sql',
        database: 'myApp',
        table: 'Category'
    },
}
});

现在的问题是: 当我使用store.add({id:123,slug:'123',title:'123',post_count:123});store.add(Ext.create('myApp.model.Category'{id:123,slug:'123',title:'123',post_count:123}); 他们都没有成功; 看来,当我删除“id:123”并只添加数据时

{slug:'123',title:'123',post_count:123}

它成功了,当我执行store.sync() 时,WebSql 数据库显示没有“id”的数据。 我确实知道,如果我将 id 更改为 'cate_id' 之类的其他内容,它将起作用。 但是,我希望这个 'id' 作为 idproperty,并且我不想更改它的名称。 我该怎么办?你如何在sencha touch中使用websql?我在 LocalStorage 中发现了同样的问题。

【问题讨论】:

    标签: extjs sencha-touch local-storage store web-sql


    【解决方案1】:

    自己回答。 模型会在创建时执行此操作

        // If it does not have an id in the data at this point, we use the one generated by the id strategy.
        // This means that we will treat this record as a phantom record from now on
        id = me.data[idProperty];
        if(!id && id !==0){
            me.data[idProperty]= me.internalId = me.id;
            me.phantom =true;
    

    如果我创建了一个带有 id 的模型,它告诉 sencha 它在数据库中有数据,这样当我同步()时,它不会被推送到数据库。

    只有当我使用 phantom = true 创建模型时,我才能将其同步()到数据库。

    这是添加记录的代码:

        getNewRecords:function(){
        returnthis.data.filterBy(function(item){
            // only want phantom records that are valid
            return item.phantom ===true&& item.isValid();
        }).items;
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      相关资源
      最近更新 更多