Created
January 4, 2012 20:16
-
-
Save davidortinau/1561886 to your computer and use it in GitHub Desktop.
Backbone View with SWF embed
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
ThisLife.ImportTemplate = _.template(' | |
<!-- omitting a bunch of html --> | |
<div id="no_flash_detected"> | |
<div class="top"> | |
<h3 class="jeff_script">No Flash Detected</h3> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
function flashReady(){ | |
return $.cookies.get("sessionToken"); | |
} | |
</script> | |
<!-- omitting a bunch of html --> | |
') |
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
ThisApp.View.ModalImport = Backbone.View.extend | |
el: $('#tl_import_modal_wrap') | |
events: | |
# omitted a bunch of events | |
"flashReady #no_flash_detected" : "flashReady" | |
initialize: -> | |
@personModel = ThisApp.Model.getPersonInfo | |
@render() | |
render: -> | |
$(@el).html ThisApp.ImportTemplate( person: @personModel ) | |
this | |
embedSWF: -> | |
if swfobject.hasFlashPlayerVersion("9") | |
currentSubdomain = ThisApp.Model.getCurrentLife().subdomain | |
flashvars = | |
environment: "staging" | |
subdomain: currentSubdomain | |
params = | |
quality: "high" | |
wmode: "opaque" | |
bgcolor: "#f9f9f9" | |
allowfullscreen: "true" | |
allowscriptaccess: "always" | |
attributes = | |
name: "uploader" | |
styleclass: "sauploader" | |
align: "middle" | |
swfobject.embedSWF "StandaloneUploader.swf", "no_flash_detected", "100%", "100%", "9.0.0", false, flashvars, params, attributes | |
else | |
$("#uploader_loading").hide() | |
flashReady: -> | |
console.log 'flashReady YES' | |
$.cookies.get("sessionToken") | |
topBarLinks: (e) -> | |
# omitted code | |
switch e.currentTarget.parentNode.id | |
when 'tl_import_top_desktop' | |
show('#tl_modal_import_desktop') | |
$("#tl_modal_import_desktop .tl_modal_import_step").show() | |
@embedSWF() | |
# ommitted the rest of the file |
TestView = Backbone.View.extend({
trace: function(data) {
console.log("ActionScript says: " + data);
}
});
function pageInit() {
testView = new TestView();
}
Using the global 'testView' you can call testView.trace from ActionScript
TestView = Backbone.View.extend({
el: $('#home'),
events: {
"flashready": "trace"
},
trace: function(event, data) {
console.log("ActionScript says: " + data);
}
});
new TestView();
function pageInit() {
console.log("pageInit");
//testView = new TestView();
}
function sendToJavaScript(value) {
$('#home').triggerHandler('flashready', value);
}
Call sendToJavaScript from ActionScript.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ModalImport view is brought into existence when a navigation button is clicked.
When the Flash swf referenced above initializes it calls flashReady via ExternalInterface. Ideally this could be all within the Backbone view, but that's not working. I took a stab at using the event wiring with a custom event, but that really doesn't make sense since Flash isn't dispatching an event like a click, it's trying to invoke a method called flashReady.
Note I have the same function in the template file in a script block and that one is in the correct scope and works fine.
So, I'm curious if there's a way to invoke functions within a Backbone View from Flash embedded within that same view's template/dom/scope.