Ok, am starting to get this working.
I created a "presentation_controller" which does the following:
- Load the JSON data "presentation" data to configure the player
- Bind the handlers for the "loaded" events fired from the components
- Create the sub-controls (loader graphic, mediaplayer, etc.)
In the "init" function of the "presentation_controller" I bind the events, load the JSON data
- init: function() {
this.bind($("#loader"), "loaded", "loadedLoader"); // Bind to the "loaded" events for loader
this.bind($("#mediaplayer"), "loaded", "loadedMediaPlayer"); // Bind to the "loaded" events for mediaplayer
Webplayer.Models.Presentation.findAll({}, this.callback('data_loaded')); // get the presentation JSON
},
I then want to create the other plugin controls (loader, mediaplayer) based on information in the returned JSON model.
The problem is, if I try to call the function that creates the loader and mediaplayer plugins in the "data_loaded" callback function that occurs after the JSON is loaded, none of the events are captured. However, if I change the order of things so the function that creates these controls is called in the "init" function, the event handlers
do properly capture the events. Of course, this won't work because I don't have the correct data to set them up, yet. How can I make sure the event handlers work even after the "data_loaded" handler is called?
This is what I am trying that
causes no events to be captured:
- // in "presentation_controller.js"
- /* @Prototype */
{ - {
- /**
- * Initialize a new Presentation controller.
- */
- init: function() {
- this.bind_events();
- Webplayer.Models.Presentation.findAll({}, this.callback('data_loaded'));
- },
-
- /*
- * Handle events from components
- */
- bind_events: function() {
- this.bind($("#mediaplayer"), "loaded", "loadedMediaPlayer");
- },
-
- /**
- * Handle the data loaded event
- */
- data_loaded: function(presentations) {
- this.isDataLoaded = true;
- this.presData = presentations[0];
- this.create_controls(); // Call the function to create the mediaplayer and other controls
- }
-
- /* ... etc. */
- }
This captures events properly, but I don't have the JSON data yet:
- // in "presentation_controller.js"
- /* @Prototype */
- {
- {
- /**
- * Initialize a new Presentation controller.
- */
- init: function() {
- this.bind_events();
- Webplayer.Models.Presentation.findAll({}, this.callback('data_loaded'));
- this.create_controls(); // Call the function to create the mediaplayer and other controls
- },
-
- /*
- * Handle events from components
- */
- bind_events: function() {
- this.bind($("#mediaplayer"), "loaded", "loadedMediaPlayer");
-
- },
-
- /**
- * Handle the data loaded event
- */
- data_loaded: function(presentations) {
- this.isDataLoaded = true;
- this.presData = presentations[0];
-
- }
-
- /* ... etc. */
- }