Skip to content

Instantly share code, notes, and snippets.

@jjaffeux
Last active December 11, 2015 07:08
Show Gist options
  • Save jjaffeux/4563746 to your computer and use it in GitHub Desktop.
Save jjaffeux/4563746 to your computer and use it in GitHub Desktop.
Event delegation rubymotion example
#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