Class: Volt::Persistors::LocalStore

Inherits:
Base show all
Defined in:
lib/volt/models/persistors/local_store.rb

Overview

Backs a collection in the local store

Instance Method Summary collapse

Methods inherited from Base

#async?, #auto_generate_id, #clear, #event_added, #event_removed, #initialize, #removed, #root_model

Constructor Details

This class inherits a constructor from Volt::Persistors::Base

Instance Method Details

#added(model, index) ⇒ Object

Called when a model is added to the collection



10
11
12
# File 'lib/volt/models/persistors/local_store.rb', line 10

def added(model, index)
  root_model.persistor.save_all
end

#changed(attribute_name) ⇒ Object

Callled when an item is changed (or removed)



33
34
35
36
37
# File 'lib/volt/models/persistors/local_store.rb', line 33

def changed(attribute_name)
  root_model.persistor.save_all

  true
end

#loaded(initial_state = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/volt/models/persistors/local_store.rb', line 14

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

#save_allObject

Called on the root



40
41
42
43
44
45
46
# File 'lib/volt/models/persistors/local_store.rb', line 40

def save_all
  return if @loading_data

  json_data = EJSON.stringify(@model.to_h)

  LocalStorage['volt-store'] = json_data
end