Declare a class inline and use it.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Declare a class inline and use it.

Jose L Martinez-Avial
Hello,
   I'm trying to define a class inline (as a script in the page) as follows:
require(["dojo/_base/declare"], function(declare){
    declare("my.support", null, {
            getToken:function() {
                return "Mytoken";
            }
    });
})

and then I'm trying to use it as follows:

require([
        "my/support"],
function(
    MySupport){
        alert(MySupport.getToken());
    }
)

but after declaring it, when I try to use it, it tries to recover my/support.js from the server. Is there any way to declare the class inline and use it?

thanks

JL

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: Declare a class inline and use it.

Kenneth G. Franqueiro
It's not entirely clear to me what you are ultimately trying to
accomplish, but I think you're confusing define and declare, which are
entirely unrelated, and require will always request modules from the
server the first time they are referenced; that's what it's supposed to do.

If both of these are within the same scope, then just do this:

var MySupport = declare(null, {
    getToken: function () {
        ...
    }
});

...

var support = new MySupport();
support.getToken();

(Also note that I created an instance, since the properties in the
object passed to declare are applied to the prototype and declare
creates/returns a constructor.  If all you want is a plain old JS
object, there's not really a reason to use declare at all.)

--Ken

On 3/24/2016 10:07 PM, JOSE L MARTINEZ-AVIAL wrote:

> Hello,
>    I'm trying to define a class inline (as a script in the page) as follows:
> require(["dojo/_base/declare"], function(declare){
>     declare("my.support", null, {
>             getToken:function() {
>                 return "Mytoken";
>             }
>     });
> })
>
> and then I'm trying to use it as follows:
>
> require([
>         "my/support"],
> function(
>     MySupport){
>         alert(MySupport.getToken());
>     }
> )
>
> but after declaring it, when I try to use it, it tries to recover
> my/support.js from the server. Is there any way to declare the class
> inline and use it?
>
> thanks
>
> JL
>
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest