Asked  7 Months ago    Answers:  5   Viewed   133 times

I've watched some videos on the topic of backbone js. This is an example straight from the video. It is from 2012, so I'm thinking backbone rules/library have changed, but I can't figure out why this does not work at the moment. In the video, the person shows it running in the JS Fiddle, but I can't get it to work. (I've included the necessary libraries in JS Fiddle, i.e. underscore, backbone and jQuery)

var V = Backbone.View.extend({
  render: function () {
  	var data = { lat: -27, lon: 153 };
    this.$el.html(_.template('<%= lat %> <%= lon%>', data));
    return this;

var v = new V();

<script src=""></script>
<script src=""></script>
<script src=""></script>



You used to be able to parse and fill in an Underscore template in one go like this:

var html = _.template(template_string, data);

But as of Underscore 1.7.0, the second argument to _.template contains template options:

template _.template(templateString, [settings])

Compiles JavaScript templates into functions that can be evaluated for rendering. [...] The settings argument should be a hash containing any _.templateSettings that should be overridden.

You have to compile the template using _.template and then execute the returned function to get your filled in template:

var tmpl = _.template(template_string);
var html = tmpl(data);

// or as a one-liner, note where all the parentheses are
var html = _.template(template_string)(data);

In your case, it would look something like this:

var V = Backbone.View.extend({
  render: function () {
    var data = { lat: -27, lon: 153 };
    var tmpl = _.template('<%= lat %> <%= lon %>');
    return this;

var v = new V();

<script src="//"></script>

<script src="//"></script>
<script src="//"></script>
Tuesday, June 1, 2021
answered 7 Months ago

Underscore _.template function takes a template string as argument (and optionally a settings object) and returns a new pre-compiled template function which takes an object as an argument.

This object is the data used within the template:

// creates a template function
var templateFunc = _.template("<span><%= name %></span>");

// render the template using the passed data
templateFunc({ name: "Émile" }); // <span>Émile</span>

By default, template places the values from your data in the local scope via the with statement. However, you can specify a single variable name with the variable setting.

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

model.toJSON() returns a shallow copy or the attributes hash of the model.

To achieve the equivalent of the above example:

var model = new Backbone.Model({ name: "Émile" });
templateFunc(model.toJSON()); // <span>Émile</span>

For Underscore.js before v1.7, the template function signature was a little different:

_.template(templateString, [data], [settings]) 

If a data object was passed, it didn't returned a function, but returned the rendered template string directly.

_.template('This is <%= val %>.', { val: "deprecated" });
// This is deprecated.
Sunday, June 20, 2021
answered 6 Months ago

You need to take the pointers by reference:

template <> 
void Max(char*& a,char*& b,char*& c) 

That said, it would be better not to use an explicit specialization and instead just overload the function:

void Max(char* a, char* b, char* c)

It's almost always a bad idea to specialize function templates. For more, see Herb Sutter's "Why Not Specialize Function Templates?"

Friday, August 6, 2021
answered 4 Months ago

Your todoData is (presumably) a string:

var todoData = model.get('title');

but a compiled Underscore template wants a key/value object as its argument:

When you evaluate a template function, pass in a data object that has properties corresponding to the template's free variables.

Looks like you have a title global variable or window property that is a <div> DOM object or you would get a ReferenceError complaining about an unknown title variable instead of a stringified DOM object.

In any case, the fix is pretty easy: give the template function what it wants:

$('#todo-list').append(compileTemplate({ title: todoData }));

or the common Backbone approach:


There are cases where the model will have optional attributes that the templates need to access. In such cases, you might have:

<%= pancakes %>

in the template but sometimes toJSON will give you:

{ title: 'x' }

and other times you'll get:

{ title: 'x', pancakes: 11 }

In such case you need to "un-optionalize" the optional attributes in your toJSON: toJSON should supply everything. If you have attributes that are optional then toJSON should ensure that it returns them with undefined or null values.

Saturday, August 21, 2021
Lawrence Taur
answered 4 Months ago

Your problem is that "global" variables in python are not actually global, but rather local to the module in which they were defined in. Thus, you cannot reference globals decalred in the file in the file.

There are three possible solutions:

  1. (not recomended) Move the declarations of screen and background to the file, which would put them seemingly out of place.

  2. (also not recommended) Add a from main import screen, background to the file, which creates circular import problems, forcing the module to do its from classes import * after defining screen and background

  3. Add a third or file, where the screen and background variabled are defined and import it from and

The mouse_pos variable is totally unnecessary and misused:

  1. You could just use pygame.mouse.get_pos() directly in the file, but
  2. Using pygame.mouse.get_pos() itself is extranious because pygame.MOUSEBUTTONDOWN events have a pos attribute pointing to the position of the mouse when the event was fired.
Tuesday, August 31, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :