Class: UrlTracker

Inherits:
Object show all
Defined in:
lib/volt/page/url_tracker.rb

Overview

The URLTracker is responsible for updating the url when a param changes, or updating the url model/params when the browser url changes.

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ UrlTracker

Returns a new instance of UrlTracker.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/volt/page/url_tracker.rb', line 5

def initialize(page)
  @page = page

  if Volt.client?
    page.params.on('child_changed') do
      @page.url.update!
    end

    that = self

    # Setup popstate on the dom ready event.  Prevents an extra
    # popstate trigger
    %x{
      var first = true;
      window.addEventListener("popstate", function(e) {
        if (first === false) {
          that.$url_updated();
        }

        first = false;

        return true;
      });
    }
  end
end

Instance Method Details

#url_updated(first_call = false) ⇒ Object



32
33
34
35
# File 'lib/volt/page/url_tracker.rb', line 32

def url_updated(first_call=false)
  @page.url.parse(`document.location.href`)
  @page.url.update! unless first_call
end