wire factory plugins: name in root of spec?

16 views
Skip to first unread message

Ben Tremblay

unread,
Dec 9, 2014, 9:15:36 AM12/9/14
to cuj...@googlegroups.com
It seems like can't put a plugin declaration in the root of the wire spec.

Am I wrong? ( in the example below assume I declared a plugin with a factory named "myCustomPlugin" in the plugins node)

Main.js
{
myCustomPlugin:{
}
}

Benjamin Tremblay

unread,
Dec 9, 2014, 10:25:04 AM12/9/14
to cuj...@googlegroups.com
Why do I want to do this?
To declare factory to build a collection of components with as little JSON as possible.

Benjamin Tremblay
> --
> You received this message because you are subscribed to a topic in the Google Groups "cujojs" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/cujojs/T7JRuqq2Hu0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to cujojs+un...@googlegroups.com.
> To post to this group, send email to cuj...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cujojs.
> To view this discussion on the web visit https://groups.google.com/d/msgid/cujojs/7562e10e-8039-41b9-8a60-f8d5996d7845%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Cavalier

unread,
Dec 10, 2014, 2:11:37 PM12/10/14
to cuj...@googlegroups.com
Hey Ben,

I think I understand your question, but I want to make sure:  Are you asking if plugins can be inherited from a parent wire spec to a child wire spec?

Currently, they can't.  The original reasoning was that plugin inheritance would create dependencies on plugins *somewhere* up the hierarchy, which could break child specs if someone refactors a parent and removes a plugin, for example.

That said, I'm not so sure I find that to be a super-convincing argument anymore :)  There are plenty of other things that are inherited from somewhere up the hierarch.  For example, component names in parent specs are visible in child specs (and thus can be $ref'd).

Typically, though, plugins are pretty compact to declare:

$plugins: ['this/plugin', 'that/plugin']

Are you using lots of plugins, such that your $plugins declaration is getting unwieldy?


On Tuesday, December 9, 2014 10:25:04 AM UTC-5, Ben Tremblay wrote:
Why do I want to do this?
To declare factory to build a collection of components with as little  JSON as possible.

Benjamin Tremblay

> On Dec 9, 2014, at 9:15 AM, Ben Tremblay <ben.tr...@gmail.com> wrote:
>
> It seems like can't put a plugin declaration in the root of the wire spec.
>
> Am I wrong? ( in the example below assume I declared a plugin with a factory named "myCustomPlugin" in the plugins node)
>
> Main.js
> {
>    myCustomPlugin:{
>    }
> }
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "cujojs" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/cujojs/T7JRuqq2Hu0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to cujojs+unsubscribe@googlegroups.com.

Ben Tremblay

unread,
Dec 10, 2014, 5:20:26 PM12/10/14
to cuj...@googlegroups.com
I am asking an even simpler question, I think.

define({
    // It looks like I have to do this to use my factory 'myPluginFactory'
    someComponent: {
        myPluginFactory: {
            customProp: 'hello'
        }
    }

    // And it looks like I can NOT do this:
    myPluginFactory: {
        customProp: 'hello'
    }

});

To unsubscribe from this group and all its topics, send an email to cujojs+un...@googlegroups.com.

To post to this group, send email to cuj...@googlegroups.com.
Visit this group at http://groups.google.com/group/cujojs.

Ben Tremblay

unread,
Dec 10, 2014, 5:23:13 PM12/10/14
to cuj...@googlegroups.com
Including the plugin for clarity:

define({
    // It looks like I have to do this to use my factory 'myPluginFactory'
    someComponent: {
        myPluginFactory: {
            customProp: 'hello'
        }
    }

    // And it looks like I can NOT do this:
    myPluginFactory: {
        customProp: 'hello'
    }

    $plugins: [{
        module: 'modules/plugins/myPluginFactory',
        pretty: true
    }]
});

Edan Schwartz

unread,
Dec 11, 2014, 10:42:33 AM12/11/14
to cuj...@googlegroups.com
Ok, I think I get what you're saying now. So you've made a factory plugin called 'myPluginFactory', and you're trying to figure out why it won't work outside of a component definition.

From what I understand, factory plugins allow you to accept configuration for creating an object. If you're factory isn't used within the context of a component, an object could be created, but there would be nothing to assign it to. There would also be no way for wire to know that you're trying to use a factory plugin, and some just creating a plain object like `context.myPluginFactory = { customProp: 'hello' }`.

Consider the built-in 'create' factory plugin.

define({
 
// This uses the create factory to create a component
 
// from the AMD module 'some/component',
 
// with args ('foo', 'bar')
 
// and assigns the created object to `context.someComponent`
  someComponent
: {
    create
: {
     
module: 'some/component',
      args
: ['foo', 'bar']
   
}
 
},

 
// this does not use the create factory,
 
// as we haven't told wire the name of the object
 
// we want to create.
 
create: {
   
module: 'some/component',
    args
: ['foo', 'bar']
 
}
});

I'm guessing you're trying to do something with your `myPluginFactory` besides creating an object.

If you need to to create an object and do some other stuff, you can bite the bullet and assign it to an arbitrary key (though a factory with side-effects has some code smell, imo). Otherwise, if you just want to activate some arbitrary behavior from your spec, without returning an object, I would just put the code in a plain ol' AMD module. I do this for registering Handlebars templateHelper from my specs:

// templateHelpers/toUpperCase.js
define
(['Handlebars', function(Handlebars) {
 
Handlebars.registerHelper('toUpperCase', function(str) {
   
return str.toUpperCase();
 
});
});

// spec.js
define
({
 
// I'm using the `module` factory,
 
// to load (and run) the toUpperCase template helper module.
  toUpperCaseHelper
: {
   
module: '
templateHelpers/toUpperCase'
  }
});


Note that I'm not accessing the 'toUpperCaseHelper' component at all. But the `module` factory has to be wrapped in an object, so wire doesn't think I"m trying to make a plain-old-js-object called 'module'.
Reply all
Reply to author
Forward
0 new messages