-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example for typed object sending
- Loading branch information
Matt Karl
committed
Jan 24, 2015
1 parent
5c1aef8
commit e5bbad5
Showing
9 changed files
with
199 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | ||
<title>Parent Window</title> | ||
<meta name="description" content=""> | ||
<style type="text/css"> | ||
body { | ||
background-color:#ccc; | ||
} | ||
</style> | ||
<script src="../components/jquery/dist/jquery.js"></script> | ||
<script src="../dist/bellhop.js"></script> | ||
<script src="js/User.js"></script> | ||
</head> | ||
<body> | ||
<h1>Example App</h1> | ||
<button id="userButton">Send User</button><br> | ||
<script> | ||
|
||
// User is an example class included | ||
// in the examples/js folder | ||
var user = new examples.User("Matt", 1); | ||
|
||
var body = $('body'); | ||
|
||
// Create the bellhop object | ||
var bellhop = new Bellhop(); | ||
|
||
// Give it a name, easier for debugging purposes | ||
bellhop.name = "[child]"; | ||
|
||
// Try to connect to the parent window, assuming we're an iframe | ||
bellhop.connect(); | ||
|
||
// Not required, but a good way to see if the | ||
// communication layer is current supported | ||
// must call connect() before this can be checked | ||
if (!bellhop.supported) | ||
{ | ||
body.append("No parent to connect to."); | ||
} | ||
else | ||
{ | ||
$("#userButton").click(function(){ | ||
bellhop.send('user', user); | ||
}); | ||
} | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(function(){ | ||
|
||
/** | ||
* Example for sending a typed object | ||
* using Bellhop | ||
* @class User | ||
* @namespace examples | ||
*/ | ||
var User = function(name, id) | ||
{ | ||
this.name = name; | ||
this.id = id; | ||
}; | ||
|
||
/** | ||
* JSON.stringify will convert this into | ||
* a special data representation | ||
*/ | ||
User.prototype.toJSON = function() | ||
{ | ||
return { | ||
name : this.name, | ||
id : this.id, | ||
// This is important and is requied | ||
// to reconstruct the class | ||
// It must contain the full class namespace | ||
// don't include "window" | ||
__classname : 'examples.User' | ||
}; | ||
}; | ||
|
||
/** | ||
* Bellhop can use fromJSON to reconstruct | ||
* the object based on the serialized version | ||
* of the data from the toJSON function. | ||
*/ | ||
User.prototype.fromJSON = function(data) | ||
{ | ||
this.name = data.name; | ||
this.id = data.id; | ||
}; | ||
|
||
// Assign to namespace | ||
window.examples = {}; | ||
window.examples.User = User; | ||
|
||
}()); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | ||
<title>Bellhop Example</title> | ||
<meta name="description" content=""> | ||
<script src="../components/jquery/dist/jquery.js"></script> | ||
<script src="../dist/bellhop.js"></script> | ||
|
||
<!-- The typed class must exist for both the child/parent --> | ||
<script src="js/User.js"></script> | ||
</head> | ||
<body> | ||
<h1>Object Bellhop Example</h1> | ||
|
||
<p>Send a strictly typed object using Bellhop. In this example the child will send an instance of "examples.User" object. This is a custom class that can be found in the "js" folder.</p> | ||
|
||
<iframe id="child" | ||
src="child-object.html" | ||
width="400" | ||
height="130" | ||
style="border:none;" | ||
frameborder="0"></iframe> | ||
|
||
<div id="output"></div> | ||
|
||
<script> | ||
|
||
// Create the bellhop and give it a name, for debugging purposes | ||
var bellhop = new Bellhop(); | ||
bellhop.name = "[parent]"; | ||
|
||
var output = $("#output"); | ||
|
||
// Connect to the iframe by padding the dom to the connect() method | ||
bellhop.connect($("#child").get(0)); | ||
|
||
// Request a highscore from the iframe and handle the response | ||
bellhop.on('user', function(e){ | ||
console.log(e.data); | ||
if (e.data instanceof examples.User) | ||
{ | ||
output.append("Received the typed object<br>"); | ||
output.append(JSON.stringify(e.data)+"<br>"); | ||
} | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains 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
This file contains 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
This file contains 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