Utilize unconventional characters in Marionette and Underscore templates to enhance your design

I am facing an issue with a model that has attributes named @id, @type, and others.

When I attempt to include <%= @id %> in a template for a Marionette.ItemView (using Underscore), I encounter the following error:

Uncaught SyntaxError: Unexpected token ILLEGAL

Even using the syntax ['@id'] does not yield the desired output.

Is it necessary for me to override the serializeData function?

Thank you

Answer №1

When using underscore templates, you need JavaScript expressions within <%= ... %>. The compiled template utilizes the with statement, allowing you to reference object properties as variables. However, the issue arises when trying to use @id which is not a valid JavaScript expression.

To resolve this, it would be best to create your own serializeData function to eliminate the @s. Alternatively, you can utilize the variable option with _.template:

By default, template inserts values from your data into the local scope using the with statement. However, you can specify a single variable name using the variable setting for faster rendering.

_.template("Using 'with': <%= data.answer %>", {answer: 'no'}, {variable: 'data'});
=> "Using 'with': no"

This allows you to access properties like <%= data['@id'] %>; however, integrating this method with Marionette might require more effort than simply cleaning up the @s in a customized serializeData function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I modify the fill color of a Cell when hovering over a Bar Chart in Recharts?

I have been rendering the chart in the following manner: <BarChart width={868} height={40} data={data} margin={{top:0, bottom: 10, left:0, right:0}} barSize={5}> <Tooltip labelStyle={{ textAlign: &apo ...

"Encountering issues with running Mongoimport on Mac due to a variable within the

I am encountering an issue while attempting to import a JSON file into MongoDB. Interestingly, when I use this specific command, the file imports successfully: mongoimport -d reps_development -c users --jsonArray --file ~/reps/scripts/mockUserData.json H ...

The combination of Heroku (Cedar) with Node, Express, and Jade is causing issues with the client-side javascript files in a subdirectory. While they work fine locally with foreman and

As a newcomer to node and Heroku, I am facing what seems like a simple permission issue. Despite my efforts, I am unable to pinpoint the exact source of the problem. In a sub-directory located one level beneath my root directory where the web.js file is s ...

What happens if setTimeout fails due to a page error?

My current setup involves using setTimeout to refresh the page, updating the jQuery template items. Here is a snippet of the relevant code: <script type="text/javascript"> var results = JSON.parse('@svgPath'.replace(/&quot;/g ...

Searching in SQL using REGEX instead of the BETWEEN operator

I have a MySQL database that contains a table of ads. One of the fields in this table stores data in JSON format, and within that JSON data is a key whose value represents the price (including decimal values). For example, the field named ad_data in the d ...

When a VueJS button is located within a div that also contains a link, clicking on

Can someone help me with my HTML issue? <a href="/someplace"> <div> <vuecomp></vuecomp> <span>Click row for more info</span> </div> </a> Following is the Vue component I am working on... ...

What is the best way to create a responsive div containing multiple images?

I am working on a slider and my approach is to create a div and insert 4 images inside it. The images will be stacked one above the other using position: absolute, with a width of 1013px, max-width of 100%, and height set to auto for responsiveness. The is ...

Executing an automated process of adding items to the shopping cart using Python without the need to have a

Does anyone know of a way to automate the add-to-cart process using Python without the need for an open browser window? I've experimented with modules like mechanize, but they lack the ability to directly interact with web elements. Currently, I&apo ...

A dynamically-generated dropdown element in the DOM is being duplicated due to JavaScript manipulation

Currently, I am dynamically adding a dropdown element to the page using javascript. The goal is for this dropdown to modify the properties displayed on a map. However, when attempting to add the element after the map loads, I encounter an issue where the d ...

I'm encountering an issue with the submission button in the wizard, I suspect it could be a problem

The submit button is not working on this wizard. I believe the issue lies within the JavaScript of the form wizard. When I click the submit button, nothing happens. Upon inspecting the submit button code, I found it to be like this... <a href="#finish" ...

Skrollr immediate pop-up notification

Can anyone help me figure out how to make text appear suddenly and then disappear using skrollr? I've been able to get it to fade in and out, but I want it to just show up without any transition. Here's the code I have so far: <div id="style" ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

Having trouble downloading the Chip component in Material-UI? Learn how to fix the download issue

I used the UI to upload a file, and now I want to download it either after some time or instantly. I tried implementing this using the <Chip> component, but it's not working. I need assistance in resolving this issue. Uploaded File: const data ...

Disposing of memory in THREE JS when switching between routes in VUE

Currently, I am delving into the world of VUE JS and working on a basic SPA that navigates through different pages. In my spare time, I have developed several THREE JS demos which unfortunately tend to slow down and eventually halt when switching between ...

Having trouble with React button conditional rendering not functioning properly

Currently, I am working on a cart application and facing an issue. The problem is that the button is not getting disabled when the quantity reaches zero. Instead, it continues to show the 'add to cart' button even when no items are left in stock. ...

Automatically transforming SQL table data to JSON format

Is there an API available to extract my data from a SQL server database in JSON format? I only need it for demonstration purposes. I came across something similar like this, but I'm not sure how it works. I have Apache installed. Could someone provi ...

com.sun.istack.SAXException2: The context does not recognize class java.util.LinkedHashMap or any class in its inheritance hierarchy

A POST request is being sent to create a resource named ContentInstance. Resource: ContentInstance. // This code snippet was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 import java.math.BigInteger; impo ...

Once it hits the fourth tab, it must not cycle back to the first one

Hi there, I'm new to JavaScript I noticed that when I click the left and right arrows, the tabs circle back to the beginning However, I would like the circle to stop. When it reaches the fourth tab, it should not go back to the first. This should also ...

Exploring the "else if" Statements in a JavaScript Calculator

I need help with modifying a calculator created by a former colleague at my workplace. Unfortunately, I haven't been able to contact them and I was hoping someone here could assist me. My knowledge of javascript is limited, so please bear with me if m ...

vue-form and vue-material are not compatible with each other

In my experience, using a Vue form on a regular HTML <input> element allows validation to work as expected. However, when I switch to using the <md-input> element, the validation no longer functions and an error message is displayed: Element ...