Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created April 17, 2011 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save atduskgreg/923663 to your computer and use it in GitHub Desktop.
Save atduskgreg/923663 to your computer and use it in GitHub Desktop.
# BPWebViewDelegate.rb
# brat-pack-macruby
#
# Created by Greg Borenstein on 4/16/11.
# Copyright 2011 __MyCompanyName__. All rights reserved.
class Project
attr_accessor :name
def initialize(name)
@name = name
end
def start
puts `say started #{self.name}`
end
def self.isSelectorExcludedFromWebScript(sel); false end
end
class BratPackDelegate
attr_accessor :web_view, :js_engine
def applicationDidFinishLaunching(notification)
path = NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("home.html")
page_url = NSURL.URLWithString(path)
web_view.mainFrame.loadRequest(NSURLRequest.requestWithURL(page_url))
web_view.frameLoadDelegate = self
@project = Project.new("My Sinatra Project")
puts @project.name
end
def webView(view, didFinishLoadForFrame:frame)
@js_engine = view.windowScriptObject # windowScriptObject
@js_engine.setValue(@project, forKey: "project")
# JIT the method, no colon at the end of the method since the selector doesn't
# take arguments.
@project.respondsToSelector("start")
@js_engine.evaluateWebScript("addProject(project.name())")
#puts "js bridge test: "
# trigger JS from Ruby
#@js_engine.evaluateWebScript("$('body').text('phase 1');")
# Evaluate JS from Ruby via the DOM
# puts @js_engine.evaluateWebScript('project.name()')
# Execute Ruby code from the DOM
#@project.start
#puts @js_engine.evaluateWebScript("project.start()")
end
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Brat Pack</title>
</head>
<script src="http://code.jquery.com/jquery-1.4.2.min.js" ></script>
<script>
$(window).load(function () {
addProject = function(name){
$("#projectList").append("<li>"+name+" [<a class='startApp' href='#' id='"+name+"'>start app</a>]</li>")
$(".startApp").click(function(){
project.start();
$("body").append("here");
});
};
});
</script>
<body>
<h1>Welcome to Brat Pack!</h1>
<h2>Project List</h2>
<ul id="projectList">
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment