Notification system, _.template(...). html is not a Function

Asked

Viewed 122 times

0

I am making a notification system and am getting the following error:

Uncaught TypeError: _.template(...).html is not a function

Follow the code here:

<script type="text/x-template" id="notifications-template">
    <ul>
        <%
        _.each(notifications, function(notification{
        if( notification.type == 1){ %>
        <li><% = notification.gig.artist.name %>has canceled the gig at <% = notifications.gig.venue at <% =notifications.gig.datetime. %></li>
            <%
        }

        }))
        %>

    </ul>

</script>
<script>
    $(document).ready(function () {
        $.getJSON("/api/notifications", function (notifications) {
            $(".js-notifications-count")
            .text(notifications.length)
            .removeClass("hide")
            .addClass("animated bounceInDown");

            $(".notifications").popover({
                html: true,
                title: "Notifications",
                content: function () {
                    var compiled = _.template("#notifications-template").html();
                    return compiled({ notifications: notifications });
                },
                placement: "bottom"
            });

        });


    });

</script>

Updating

It was missing the '$' selector inside _.template($("#notificação").html()

But now I get an error in underscore.js

Uncaught SyntaxError: Unexpected token {
  • The function _.template is there? This .html() at the end is part of it? Try to take out . html()

  • '#notification-template' This is appearing there in the notifications, and it was supposed to appear the notifications, and this _.template is from a library or "underscore.js"

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings, as we usually keep them as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the Stack Overflow chat in Portuguese. If you have questions about the operation, rules and procedures of the site visit the Stack Overflow in English Meta :)

  • Missed close the new parenthesis you opened

  • It wasn’t like that, I’ve been trying for a while, but it’s giving n.replace is not a Function, I’ve reinstalled the library and nothing

  • When you included the selector symbol ($), you closed the template method parentheses? _.template($("#notificação").html());

Show 1 more comment

1 answer

0


The syntax of both libraries is incorrect:

According to the documentation of underscore.js

You need to send an HTML element as a parameter to _.template();

For example:

var template = _.template("<b><%- value %></b>");

and select an HTML element via jQuery using:

function . html() jQuery

$("#notifications-template").html()

And in your case you put:

_.template("#notifications-template").html();

It would be right to put:

_.template($("#notifications-template").html());

And if id element notifications-template exist it will return you the HTML.

At the beginning of <ul> we have some syntax errors in function closures and Interpolate <% %> <%= %> <%- %>, try to use this code:

<ul>
    <%
        _.each(notifications, function (notification){
            if(notification.type == 1){ 
                <li><%= notification.gig.artist.name %> has canceled the gig at <%= notifications.gig.venue at%> <%=notifications.gig.datetime.%></li>
            }
        });
    %>
</ul>

To understand more about using the template see these examples:

Example 1

Example 2

Example 3

And we also have this question in the stack overflow which is very useful, that’s where I got the jsfiddle fonts from: Source

I believe above we have everything you need to solve your problem, hope to have helped.

  • Dude, it wasn’t, I saw it, I actually checked it the same day, but now I’m taking 'underscore-min.js:Formatted:969Uncaught Syntaxerror: Unexpected token {(... )' there in the library itself underscore.js , I don’t know what else to do :( already regressed the version, I’ve done the devil to 4 and it didn’t help

  • What do you have in your Notifications variable? Are you sure you should pass the entire variable inside the: Compiled({ Notifications: Notifications });? I believe you should only include an attribute of this variable, because you will return an HTML template in _.template and apply it to some attribute that is within Notifications.

  • Yes, that’s correct, as you can see for each item within Notifications it will create that item up there, I’m guessing it’s the syntax up there that’s wrong, could you help me? where it says: .each(notifications, function(notification{&#xA; if( notification.type == 1){ %>&#xA; <li><% = notification.gig.artist.name %>has canceled the gig at <% = notifications.gig.venue at <% =notifications.gig.datetime. %></li>&#xA; <%&#xA; } I don’t think so and I’m not finding help

  • I edited the post, check it out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.