NealR
NealR

Reputation: 10709

Uncaught TypeError: Cannot read property 'Constructor' of undefined

I am getting this and a couple other weird error messages on my main ASP MVC 3 layout page.

I am using the following scripts

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/typeahead.js")" type="text/javascript"></script>

And trying to get the typeahead.js to work in our nav bar. Below is the code I currently have on my machine. I had this working at one point in time, however after a sync up with a coworker the typeahead search does not work anymore and I haven't been able to track down the issue.

The first line in the code below gives the following error

Uncaught TypeError: Cannot read property 'Constructor' of undefined

If I comment out the protoype.blur function, however, I then get this error

Uncaught TypeError: Object [object Object] has no method 'typeahead' 

Which points to the $('.AgentSearch .typeahead).typeahead function. Been stuck on this for hours, any help would be greatly appreciated. Thanks

        // Workaround for bug in mouse item selection
        $.fn.typeahead.Constructor.prototype.blur = function () {
            var that = this;
            setTimeout(function () { that.hide() }, 250);
        };

        $('.AgentSearch').typeahead({
            source: function (term, process) {
                var url = '@Url.Action("GetAgents", "Agent")';
                var agents = [];
                map = {};
                return ($.getJSON(url, { term: term }, function (data) {
                    $.each(data, function (i, item) {
                        map[item.Name] = item;
                        agents.push(item.Name);
                    });
                    process(agents);
                }));
            },
            highlighter: function (item) {
                var p = map[item];
                var display = ''
                             + "<div class='typeahead_wrapper'>"
                             + "<div class='typeahead_labels'>"
                             + "<div class='typeahead_primary'>" + p.Name + "</div>"
                             + "<div class='typeahead_third'><i>LastFour:</i> " + p.LastFour + "</div>"
                             + "</div>"
                             + "</div>";
                return display;
            },
            updater: function (item) {
                window.location.href = ("/Monet/Agent/Details/" + map[item].SymetraNumber);
            }
        });

Here is the search box I'm binding this to

            <form class="navbar-search pull-left">   
                <input type="text" name="names" value="" id="AgentSearch" class="search-query" data-provide="typeahead" placeholder=" Agent Search"/> 
            </form>

EDIT

Here are the script tags as they're rendered by the browser

<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/Scripts/typeahead.js" type="text/javascript"></script>

Upvotes: 3

Views: 17253

Answers (1)

theUtherSide
theUtherSide

Reputation: 3476

It looks like typeahead is not defined.

If you're trying to extend typeahead, this article may help: Extend the bootstrap-typeahead in order to take an object instead of a string

$.fn will always return an object ('[]'), this is why the error change when you comment-out .prototype.blur, but they're both indicating 'typeahead' doesn't exist.

Just a guess, but it could be a synchronicity issue where your code is running before the library loads.

Upvotes: 0

Related Questions