TypeError: JS.Class is not a constructor

2,046 views
Skip to first unread message

Patrik Gmitter

unread,
Oct 29, 2012, 6:40:34 PM10/29/12
to jsclas...@googlegroups.com
Hi, im trying create class.. but i have problem on the start :-)


index.php // loader-browser.js - loaded ok
...
    <script type="text/javascript" src="/bundles/web/js/jquery.
js">
    <script type="text/javascript">
    $JSCLASS_PATH = './bundles/web/js/jsclass/';
    </script>
    <script type="text/javascript" src="/bundles/web/js/jsclass/loader-browser.js">
   <script type="text/javascript" src="/bundles/web/js/test.js">
....
---------------------------------------------------------------------------

test.js

   JSCLASS_PATH = '/bundles/web/js/jsclass/';

    var Animal = new JS.Class({
        initialize: function(name) {
            this.name = name;
        },

        speak: function(things) {
            return 'My name is ' + this.name + ' and I like ' + things;
        }
    });

---------------------------------------------------------------------------

console output

TypeError: JS.Class is not a constructor
[Break On This Error] speak: function(things) {

James Coglan

unread,
Oct 29, 2012, 6:49:31 PM10/29/12
to jsclas...@googlegroups.com
The problem with the code below is that you don't actually load JS.Class -- either add jsclass/core.js after loader-browser.js or use JS.require('JS.Class', function() { ... }) to load it.

Patrik Gmitter

unread,
Oct 29, 2012, 6:53:12 PM10/29/12
to jsclas...@googlegroups.com
ah JS.Class in require.. im tried JS.Core... :) THANK YOU
Message has been deleted

Patrik Gmitter

unread,
Oct 29, 2012, 7:36:13 PM10/29/12
to jsclas...@googlegroups.com
and can define module/bundle in My namespace ? (sry i dont want create many posts)

i trying something like that - but dont work


console output

Error: Expected package at /bundles/web/js/model/class.cart.js to define My.Cart

-------------------------------------

JS.Packages(function () {
    with (this) {

        file("/bundles/web/js/lib/jquery.js")
            .provides('jQuery');

        file("/bundles/web/js/model/class.cart.js")
            .provides("My.Cart")
            .requires("JS.Class")
            .requires("jQuery");


    }
});

JS.require('JS.Class', 'My.Cart', function () {


--------------------------------------------------------------------
class.cart.js


var My = My || {};

JS.require('JS.Class', function () {

    My.Cart = new JS.Class('CartClass',{

.....

James Coglan

unread,
Oct 29, 2012, 7:44:26 PM10/29/12
to jsclas...@googlegroups.com
On 29 October 2012 23:34, Patrik Gmitter <pa...@patie.info> wrote:
and can define module/bundle in My namespace ? (sry i dont want create many posts)

var My = My || {};

JS.require('JS.Class', function () {

    My.Cart = CartClass = new JS.Class('CartClass',{

Try removing the JS.require() line from this file. It's asynchronous and may mean that when the file is done executing, My.Cart will not be defined. You've already expressed the dependency in your package manifest so you don't need to do it in the source files as well. 

Patrik Gmitter

unread,
Oct 29, 2012, 7:49:25 PM10/29/12
to jsclas...@googlegroups.com
i remove

JS.Packages(function () {
    with (this) {

        file("/bundles/web/js/lib/
jquery.js")
            .provides('jQuery');

        file("/bundles/web/js/model/class.cart.js")
            .provides("My.Cart")
            .requires("JS.Class")
            .requires("jQuery");


    }
});

now its only in manifest,js

but always the same error.. its possible going with this like namespaced way ?

Patrik Gmitter

unread,
Oct 29, 2012, 7:52:48 PM10/29/12
to jsclas...@googlegroups.com
ok problem was in var My = My || {}; ... with My = My || {};without var works perfect :)

Anthony Rowlands

unread,
Oct 29, 2012, 7:53:44 PM10/29/12
to jsclas...@googlegroups.com
Since I had very similar issues when I started using JS.Class, I'll quickly point you to a conversation James and I had on this list titled "How to integrate JS.Class" and his related blog article about JS.Packages which clears a lot of this up with examples. I highly recommend reading the blog to get insight into the design decisions that went into the package loading system.

Good luck,
Anthony

Patrik Gmitter

unread,
Oct 29, 2012, 8:04:44 PM10/29/12
to jsclas...@googlegroups.com
thanks :)

Patrik Gmitter

unread,
Oct 29, 2012, 8:21:36 PM10/29/12
to jsclas...@googlegroups.com
last one question

its possible loading jquery plugins with JS.require or its better to load it classic with <script ... ?

for example https://github.com/needim/noty/blob/master/js/noty/jquery.noty.js

James Coglan

unread,
Oct 29, 2012, 9:05:28 PM10/29/12
to jsclas...@googlegroups.com
On 30 October 2012 00:21, Patrik Gmitter <pa...@patie.info> wrote:
last one question

its possible loading jquery plugins with JS.require or its better to load it classic with <script ... ?

for example https://github.com/needim/noty/blob/master/js/noty/jquery.noty.js

Sure, just state which object they provide.

file('noty.js').provides('jQuery.noty').requires('jQuery');

JS.require('jQuery.noty' ...) 

Patrik Gmitter

unread,
Oct 30, 2012, 3:55:14 AM10/30/12
to jsclas...@googlegroups.com
yes i tried this but

- when i have        

        file("/bundles/web/js/plugin/jquery.noty.js")
            .provides('jQuery.noty')
            .requires("jQuery");

---- use noty({ type:'success', .... // like in noty manual

ReferenceError: noty is not defined
noty({  type:'success', ...


----------------

-use  jQuery.noty({ type:'success', ....

TypeError: jQuery.noty is not a function
...ery.noty({  type:'success', ...


--------------

wen i load it with <script ... of cource works as in manual say - noty({ type:'success', ...

Patrik Gmitter

unread,
Oct 30, 2012, 5:11:40 AM10/30/12
to jsclas...@googlegroups.com
when i hack noty.js

BEFORE

// Helpers
function noty(options) {

AFTER

// Helpers
noty =function noty(options) {


and use

        file("/bundles/web/js/plugin/jquery.noty.js")
            .provides('noty')
            .requires("jQuery");

IT WORKS... But too bad that I had to modify the plugin code.. this will case problem with updates.. its there other way to do it ? thx
Reply all
Reply to author
Forward
0 new messages