2014-07-16 30 views
0

這是我的模型:我如何在煎茶觸摸使用的WebSQL唯一ID

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更改爲「cate_id」之類的其他名稱,它將起作用。 但是,我希望將'id'作爲id屬性,並且我不想更改它的名稱。 我該怎麼辦?如何在sencha touch中使用websql?我在LocalStorage中發現了同樣的問題。

回答

0

自己回答。 創建

// 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的模式,它的幻象是假的,告訴它在數據庫是數據的煎茶,這樣,當我同步(),它不會被推送到數據庫。

只有當我用phantom = true創建模型時,纔可以將其同步()到數據庫。

這裏是addrecords代碼:

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