Created
July 16, 2014 08:38
-
-
Save rvanzon/442f16ea7db5f7b03fc3 to your computer and use it in GitHub Desktop.
RHashManager, connect location.has to CPNotificationCenter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // RHashManager | |
| // by Richard van Zon (c) Relectus | |
| // | |
| // Public Domain | |
| // | |
| @import <Foundation/CPObject.j> | |
| var sharedHashManager = nil; | |
| @implementation RHashManager : CPObject | |
| { | |
| CPString currentHash @accessors(); | |
| } | |
| + (id)sharedManager | |
| { | |
| if (!sharedHashManager) | |
| sharedHashManager = [[self alloc] init]; | |
| return sharedHashManager; | |
| } | |
| - (id)init | |
| { | |
| if (self = [super init]) | |
| { | |
| window.onhashchange = function() | |
| { | |
| [self hashdidChange]; | |
| } | |
| } | |
| return self; | |
| } | |
| - (void)setup | |
| { | |
| [self setCurrentHash:window.location.hash]; | |
| } | |
| - (CPString)hash | |
| { | |
| return window.location.hash.substr(1); | |
| } | |
| - (CPArray)segments | |
| { | |
| return window.location.hash.substr(1).split(','); | |
| } | |
| - (CPString)firstSegment | |
| { | |
| return window.location.hash.substr(1).split(',')[0]; | |
| } | |
| - (void)setHash:(CPString)theHash | |
| { | |
| [self setCurrentHash:theHash]; | |
| window.location.hash = @" "; | |
| window.location.hash = theHash; | |
| } | |
| - (void)hashdidChange | |
| { | |
| if (window.location.hash != [self currentHash]) | |
| { | |
| [self setCurrentHash:window.location.hash]; | |
| [[CPNotificationCenter defaultCenter] postNotificationName:@"kHashDidChange" object:[self segments]]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment