Last active
December 11, 2015 07:08
-
-
Save jjaffeux/4563746 to your computer and use it in GitHub Desktop.
Event delegation rubymotion example
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
#example | |
class Request | |
attr_accessor :delegate | |
def self.initWithPath(*args, &block) | |
instance = allocate | |
instance.myInitializer(*args, &block) | |
instance | |
end | |
def myInitializer(path) | |
@path = path | |
end | |
def start | |
self.delegate.public_send(:requestDidStart) | |
someMethodWithPath(@path) do |result| | |
self.delegate.public_send(:requestDidSucceed, result.body) if result.success? | |
self.delegate.public_send(:requestDidFail, result.error) if result.error? | |
end | |
end | |
end | |
#usage | |
class SomeViewController < UIViewController | |
def viewDidLoad | |
request = Request.initWithPath('/pictures') | |
request.delegate = self | |
request.start | |
end | |
def requestDidStart | |
p "ZOMG request did start !!!!" | |
end | |
def requestDidSucceed(body) | |
p "do something boring with #{body}" | |
end | |
def requestDidFail(error) | |
p "Oh noes..." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment