0

First, I am using an MVC WebAPI project. My scripts are configured in the bundle config and then rendered on the page (Index.cshtml).I am trying to create a JQuery widget using the widget factory. I have created the 'test' javascript file with this content:

$.widget("custom.test", {
    options: {
        value: 0
    },
    _create: function () {
        this.options.value = this._constrain(this.options.value);
        this.element.addClass("test");
        this.refresh();
    },
    _setOption: function (key, value) {
        if (key === "value") {
            value = this._constrain(value);
        }
        this._super(key, value);
    },
    _setOptions: function (options) {
        this._super(options);
        this.refresh();
    },
    refresh: function () {
        var testVal = this.options.value + "%";
        this.element.text(testVal);
        if (this.options.value == 100) {
            this._trigger("complete", null, { value: 100 });
        }
    },
    _constrain: function (value) {
        if (value > 100) {
            value = 100;
        }
        if (value < 0) {
            value = 0;
        }
        return value;
    },
    _destroy: function () {
        this.element
            .removeClass("test")
            .text("");
    }
});

In the Index file I render the bundles:

@Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/lib")

in the head of the file (NOTE: My widget script file is in the lib bundle). Then, after the rendering code, I added a script tag and added this:

<script>
            $(function() {
                //OTHER CODE

                var test = $("<div></div>")
                    .appendTo("body")
                    .test();

                test.option( "value", 50 );

                alert(test.options.value);
            });

        </script>

There is other code in the this script tag so I know at least some of the code works. Now, when I load the page I get an error (it directs me to the $.widget("custom.test", ...) portion of the code) saying "Uncaught TypeError: undefined is not a function". I can see, when I look at the source, that the javascript files are being included in the correct order.

Does anyone have any idea why this is happening and how to fix it?

EDIT

There was a syntax issue with how I was attempting to access information from the widget. The proper code is:

            var something = $(".test").colorize();
            something.colorize("option", "value", 100);
            var test = something.colorize("option", "value");
            alert(test);

            something.colorize("random");
            alert(something.colorize("option", "red"));
4
  • Does it all work without bundling? Commented Nov 25, 2014 at 21:45
  • No it does not. I'm not sure where to go from here. Commented Nov 26, 2014 at 17:06
  • 1
    I would say then its got nothing to do with the bundling, and its an error in the code. Include the JS unbundled and unminified, and (assuming your using Visual Studio), use IE (must be IE) put a breakpoint in the create of your widget and step through the code until you get to the error. undefined is not a function will be the error from you trying to execute a method on an object that is null, just need to find which one... OR add some alerts or console.log to points in the code and find exactly where the error is occuring. Also VS will only debug JS in JS files, (i.e. not on the page) Commented Nov 26, 2014 at 19:18
  • There was indeed an error in the usage of the widget. I have learned how to properly use the widget and am editing. Commented Dec 1, 2014 at 16:35

1 Answer 1

0

If you're concatenating javascript files, make sure they are ending with (jQuery); and not just (jQuery) - note the semicolon

If your bundled JS is not minified - the line number the uncaught typeerror cites should lead you to this.

(This is the case 99% of the time I see this and the developer is combining files - though it is not always the case)

Sign up to request clarification or add additional context in comments.

2 Comments

I am not concatenating them. The bundles just write script tags in the HTML.
Does it give you a line number? Whats at that line number? Can you expand it for a stack trace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.