Search code examples
ruby-on-rails-3.2local-storagespine.js

How we use Local storage (Spine.Model.Local) in spine rails app


I am trying to build a app with rails & spine that support offline browsing. I am facing problem in use of Spine.Model.Local in my one of model. Can any explain me that how we use Spine.Model.Local in our rails & spine app. Thanks


Solution

  • First read full Spine Documentation for creating spine App.

    After spending a lot of time i done it and store my rails model data into a specific spine model and use it locally for fast response.

    First :: Define your spine model as below..

    class App.ModelName extends Spine.Model
      @configure 'ModelName', 'columeName1', 'columeName2', ....(and other column)
    
      @extend Spine.Model.Local
      @extend Spine.Model.Ajax
    
      # other methods, variables, etc.
      # define your methods
    

    Second :: Now sync data of specific model through your rails with ajax and put it in local storage like below...

    //Request for sync model data for current login user
        Spine.Ajax.queue(function() {
            $.ajax({
                contentType : "application/json",
                dataType : "json",
                headers : {
                    "X-Requested-With" : "XMLHttpRequest"
                },
                url : "/yourDesiredURL.json",
                type : "get",
                success : function(data, status, xhr) {
                    for (key in data) {
                        window.localStorage[key] = JSON.stringify(data[key])
                    }
                    new App({
                        el : $("#app")
                    });
                },
                error : function(xhr, statusText, error) {
                    // Do what do you want to do
                }
            });
        }); 
    

    Now your desired model data filled into local storage and ready to play with Spine (javascript MVC framework).