Can't resolve dependency "epi.globalcommandregistry" when setting up a custom command

Member since: 2004

I have the following javascript in the file ClientResources/Scripts/widgets/media/Mediacommands.js:

require(['dojo/aspect', 'epi/dependency', 'samples/media/MediaUpdateCommand'], function (aspect, dependency, MediaUpdateCommand) {

    //alert('registering');
    var handle,
		key = 'epi.cms.globalToolbar',
		registry = dependency.resolve('epi.globalcommandregistry'),
		callback = function (identifier, provider) {
		    if (identifier !== key) {
		        return;
		    }

		    provider.addCommand(new MediaUpdateCommand(), {
		        showLabel: true
		    });

		    handle.remove();
		};

    handle = aspect.after(registry, 'registerProvider', callback, true);
});

My module.config looks like this:

<module>
    <assemblies>
        <add assembly="JR.Web.CMS" />
    </assemblies>
  <clientResources>
        <add name="epi-cms.widgets.base" path="Styles/Styles.css" resourceType="Style"/>
        <add name="epi-cms.widgets.base" path="Scripts/widgets/media/MediaCommands.js" resourceType="Script"/>
  </clientResources>
  <dojo>
    <paths>
      <add name="samples" path="Scripts/widgets" />
    </paths>
  </dojo>
  <moduleDependencies>
    <add dependency="Shell" />
  </moduleDependencies>
</module>

This is intended to add a button to the Editor toolbar, and *sometimes* works. However around 70% of the time I see the error Can't resolve dependency "epi.globalcommandregistry" in the console.

This corresponds to the line registry = dependency.resolve('epi.globalcommandregistry'), in the javascript above.

Is there any reason why the dependency can't be resolved at some times and more to the point, how can I ensure it's resolved every time?

Thanks,

Mark

#119097 Mar 20, 2015 15:51
  • Member since: 2002

    Hi!

    I suggest that you create an init module on the client since this will make the code run at the right time. Here is a copy paste example from something I worked on recently:

    Module.config:

    <clientModule initializer="[your module class including namespace]">
    <moduleDependencies>
    <add dependency="CMS" type="Require RunAfter" />
    </moduleDependencies>
    </clientModule>

    Client module sample:

    define([
        // Pull in the patches. Not used here, just evaluated, since this is the first module loaded when starting the application.
        "dojo/_base/declare",
        "dojo/_base/lang",
        "epi/_Module",
        "epi/dependency",
        "instantTemplates/AddItemFromTemplateCommandProvider"
    ],
    
    function (
        declare,
        lang,
        _Module,
        dependency,
        AddItemFromTemplateCommandProvider
    ) {
    
        return declare([_Module], {
            // summary:
            //		Template module implementation.
            //
            // tags:
            //      internal
    
            _settings: null,
    
            constructor: function (settings) {
                this._settings = settings;
            },
    
            initialize: function () {
    
                var commandregistry = dependency.resolve("epi.globalcommandregistry");
    
                //We need to wait for the viewsettings to initialized before creating the global toolbar command provider
                commandregistry.registerProvider("epi.cms.globalToolbar", new AddItemFromTemplateCommandProvider({templatesRoot : this._settings.templatesRoot}));
            }
        });
    });
    



    #119233 Mar 25, 2015 8:22
  • Member since: 2004

    Linus,

    That did it for me (specifically the module.config settings) - thanks!.

    #119583 Mar 30, 2015 12:57