简体   繁体   English

IndexedDB object 到 jQuery 自定义触发事件?

[英]IndexedDB object to jQuery custom trigger event?

I am working on IndexedDB functionality in JS - i would like to add some custom jquery trigger event with object of IndexedDB upon success callback, not sure whether it is possible but when i test event fired but it doesn't pass IndexedDB object我正在使用 JS 中的 IndexedDB 功能 - 我想在成功回调时添加一些自定义 jquery 触发事件和 IndexedDB 的 object,不确定是否可能但是当我测试事件触发但它没有通过 IndexedDB object

            if ( !window.indexedDB ) {
                console.log( `Your browser not support IndexedDB` ) ;
                return ;
            }
            const request = indexedDB.open( 'pdr' , 1 ) ;
            request.onupgradeneeded = ( event ) => {
                let db = event.target.result ;
                let store = db.createObjectStore( 'CanvasObjects' , {
                    autoIncrement : true
                } ) ;
            } ;
            request.onerror = ( event ) => {
                console.error( `Database error: ${event.target.errorCode}` ) ;
            } ;

            request.onsuccess = ( event ) => {
                const db = event.target.result ;
                console.log(db);
                jQuery( document ).trigger( 'test_db_connected' , db ) ;

            } ;

After that i perform function on test_db_connected jquery event with following code之后,我使用以下代码在test_db_connected jquery 事件上执行 function

jQuery( document ).on( 'test_db_connected' , function ( db ) {
    console.log( 'testing_db' ) ;
    console.log( db ) ; 
    const txn = db.transaction( 'CanvasObjects' , 'readwrite' ) ; 
   } ) ;

but with above code it create error但是使用上面的代码会产生错误

Uncaught TypeError: db.transaction is not a function未捕获的类型错误:db.transaction 不是 function

Further check i figure out that whatever object return on request.onsuccess is not same in jQuery custom trigger event.进一步检查我发现无论 object 返回 request.onsuccess 在 jQuery 自定义触发事件中都不相同。

Is there any possibilities to pass IndexedDB object to the custom jquery trigger event?是否有可能将 IndexedDB object 传递给自定义 jquery 触发事件?

Appreciate your response.感谢您的回复。

You are not using jQuery's event methods correctly.您没有正确使用 jQuery 的事件方法。 The handler accepts additional parameters for any custom data you send.处理程序接受您发送的任何自定义数据的附加参数。 So your handler should look like this:所以你的处理程序应该是这样的:

jQuery(document).on('test_db_connected', function(ev, db) {
  console.log('testing_db');
  console.log(db);
  const txn = db.transaction('CanvasObjects', 'readwrite');
  console.log(txn);
});

Additionally, you really should use jQuery's Event object for constructing and triggering custom event.此外,您确实应该使用 jQuery 的Event object 来构建和触发自定义事件。 The docs state that the String event type parameter is to be used for DOM level events.文档state 将字符串事件类型参数用于 DOM 级别事件。 But this still works fine.但这仍然可以正常工作。

Working Solution工作解决方案

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

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