Sha256: 936cb0482e0b59176d63ebce6b7e4cc192e1a2e99d3ffff743cc0e0287070189

Contents?: true

Size: 1.19 KB

Versions: 24

Compression:

Stored size: 1.19 KB

Contents

require 'volt/models/persistors/base'
require 'volt/utils/local_storage'
require 'volt/utils/ejson'

module Volt
  module Persistors
    # Backs a collection in the local store
    class LocalStore < Base
      # Called when a model is added to the collection
      def added(model, index)
        root_model.persistor.save_all
      end

      def loaded(initial_state = nil)
        super
        # When the main model is first loaded, we pull in the data from the
        # store if it exists
        if @model.path == []
          json_data = LocalStorage['volt-store']
          if json_data
            root_attributes = EJSON.parse(json_data)

            @loading_data = true
            root_attributes.each_pair do |key, value|
              @model.send(:"_#{key}=", value)
            end
            @loading_data = nil
          end
        end
      end

      # Callled when an item is changed (or removed)
      def changed(attribute_name)
        root_model.persistor.save_all

        true
      end

      # Called on the root
      def save_all
        return if @loading_data

        json_data = EJSON.stringify(@model.to_h)

        LocalStorage['volt-store'] = json_data
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
volt-0.9.7.pre8 lib/volt/models/persistors/local_store.rb
volt-0.9.7.pre7 lib/volt/models/persistors/local_store.rb
volt-0.9.7.pre6 lib/volt/models/persistors/local_store.rb
volt-0.9.7.pre5 lib/volt/models/persistors/local_store.rb
volt-0.9.7.pre3 lib/volt/models/persistors/local_store.rb
volt-0.9.7.pre2 lib/volt/models/persistors/local_store.rb
volt-0.9.6 lib/volt/models/persistors/local_store.rb
volt-0.9.6.pre3 lib/volt/models/persistors/local_store.rb
volt-0.9.6.pre2 lib/volt/models/persistors/local_store.rb
volt-0.9.6.pre1 lib/volt/models/persistors/local_store.rb
volt-0.9.5 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre12 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre11 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre9 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre8 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre7 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre6 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre5 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre4 lib/volt/models/persistors/local_store.rb
volt-0.9.5.pre3 lib/volt/models/persistors/local_store.rb