开发者

how to sync web sql database

I am using ruby on rails for developing a website. I want my website to be available for offline. For that I am using Web SQL data开发者_如何转开发base. I want to know how I can synchronise the online and the offline database. The offline database is created by javascript. These are the few things I tried. To show the content of the database I am taking the data into a ruby object in the controller as shown

  @contacts = Contact.order("contacts.position ASC")

I am trying to access this object in javascript in the view file as shown below

<script type="text/javascript">

var js_obj = <%= @contacts.to_json %>;

</script>

Is this the right way? How I can view the data in js_obj variable


I would definitley have a fallback for indexedDB as well since more browser will be implementing it (ie9, Chrome, etc.)


I hate inline scripts. That being said, In the case of using offline databases an ajax request to the controller with a method that returns the .to_json is not viable on its own. Rather than using a javascript variable and assuming you are using the deprecated web SQL databases since they are currently (and strangely) the best supported standard for offline db's, I would use something like this in your script (please excuse my poor sql and keep in mind its a work in progress). So far the code is not working but I think the idea is sound.

http://jsfiddle.net/Nevraeka/hvT6u/5/


There is a project called persistence.js (persistencejs.org) which is a javascript object-database mapper with database-independent abstractions. This way you don't have to restrict your app to WebSQL databases and you can also support (with the same code) the use of the Local Storage, for example.

They also have a plugin called persistence.sync which syncs the remote database with the server's one. They have an example back-end but is written for node.js. However, you can make one for Rails following their instructions. The Rails server-side should be something like this.

Also, if you decide to use this library, you should read the limitations mentioned on the docs:

  • The synchronization library synchronizes on a per-object granularity. It does not keep exact changes on a per-property basis, therefore conflicts may be introduced that need to be resolved
  • It does not synchronize many-to-many relationships
  • Error handling is not implemented
  • Deletes are not supported but maybe you can work around the issue using a "deleted" flag
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜