Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:49:14+00:00 2026-06-06T04:49:14+00:00

I’m sure inner require isn’t the correct term, but it’ll make sense here shortly.

  • 0

I’m sure “inner require” isn’t the correct term, but it’ll make sense here shortly. I’m working on the Dijit tutorials from the toolkit’s site, and I’m running into an issue that I think is more a Javascript understanding than a Dojo one.

My page init script:

require(['player', 'dojo/dom', 'ready'], function(Player, dom){
    var p = new Player({
        type : 'video',
        dimensions : [720, 480]
    });
    p.setSource('videos/myvideo.webm')

    p.placeAt(dom.byId('stage') )
})

And my constructor of the dijit “player.js”

constructor : function(opts){
    (function($){
        require(['sg/player/component/Video', 'sg/player/component/Audio'], function(Video, Audio){
            $._setMedia( (opts.type == 'video') ? new Video() : new Audio());
            console.log($._media) // outputs an object "a"
        })
    })(this);
    console.log(this._media) // also outputs the correct object, "a"
}

// the internal setter function used above
_setMedia : function(m){
    this._media = m;
},

(The reason I have an anonymous function there is I don’t like assigning var self = this as inside the require blocks, this isn’t the containing object.)

When I create the new Player() object in the init script, I can see that a new instance of either Audio or Video is being assigned correctly. However, when I call the p.setSource() in the init script, I get an error that _media is null!

Steps

  • Create Player
    • Parse configuration options (type, dimensions, etc). Internal _media object holds the instance of Video or Audio
  • Allow setter method access outside the Player source !! ERROR !!

So my question, hopefully I gave enough context, but why the _media variable losing it’s value? Any methods inside the Player instance to be accessed from outside the Dijit’s source shouldn’t have any effect on the scope of internal variables, but that’s what it seems is happening. After the constructor returns, _media should be set! But using

setSource : function(s){
    console.log('Setting source: ', s, this._media)
    // outputs ("Setting source: path/to/video.webm', null)
},

…throws an error as the _media variable it’s referencing inside setSource is supposedly null.

Hope that’s clear 🙂

Update

Wish I could give you guys both the checkmark! Thank you for taking the time to help out.

@Frode: There definitely were some async issues that forced me to learn and try more with the structure, all of which failed, leading to this update. I think at some points the files were cached, leading to inconsistent variable content.

@phusick: I hybridized your suggestion which is posted below.

I thought about redoing the structure of how the Player would be instantiated, the object arg and the like, but decided to do the following, in case anyone hits this issue…

I combined the Audio and Video classes into the _Media file, using this structure (code removed for brevity)

define(['dojo/_base/declare'], function(declare){
    var _base = declare("_Media", null, {        
        constructor : function(type){
            this._type = type;
        },
        // etc
    })
    return {
        Video : function(){
            return declare("Video", _base, {
                constructor : function(){
                    this.inherited(arguments, ['video'])
                }
                // etc
            })()
        },
        Audio : function(){
            return declare("Audio", _base, {
                constructor : function(){
                    this.inherited(arguments, ['audio'])
                }
                // etc
            })()
        },
    }
})

…that way there’s only one file being loaded initially and it contains both subclasses. Better than loading 2 separate files when one won’t be used, IMO.

For the player type instance it then became:

this._media = opts.type && opts.type == 'video' ? new Media.Video() : new Media.Audio();

So far so good! Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:49:15+00:00Added an answer on June 6, 2026 at 4:49 am

    After the constructor returns, _media should be set!

    I think this is where you take a wrong step. Keep in mind that require is an asynchronous function!

    However, I’m a little confused by this line in your constructor (specifically, the comment):

    console.log(this._media) // also outputs the correct object, "a"
    

    Are you 100% sure this outputs “a” and not a null? If you are, please ignore the rest of my answer, because then I’ve misunderstood something 🙂

    Ok, if you’re still reading, I’ll try to explain asynchronous require. When you call:

    require([".../Video",".../Audio"], function(Video, Audio) {
        // do something and set _media
    });
    

    you are basically saying: “Browser, you go fetch the Video and Audio modules for me in the background, while I continue with my next line of code. When you’ve fetched them, run that function I gave you which sets _media.”

    In other words, it may take a long time before require() finishes and _media is set, but your code continues right away. So when you call setSource, require() may not be done yet (in fact, it may not even have started downloading anything).

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.