JsViews: Converter before helper function in data-link - javascript

Using JsViews is it possible to have a converter precede a helper function in a data-link property? Somehow like this..
<div data-link="{myConverter:~myFunction('param1'):}"></div>
Thanks!

If you mean that you want the converter to convert the value of the param1 field of your data object, and then pass it to the myConverter function, then no. The converter will process the result of the expression ~myFunction(...).
But you can create an equivalent helper function to your converter, and then chain the helpers:
<div data-link="~myFunction(~myConvert(param1))></div>"
BTW this will be data linked so that it updates when the param1 field changes. - I'm not sure if you meant to put quotes around 'param1'. If 'param1' is a string literal you are passing in, then data-linking to it won't have any reason to update, so you could just write:
<div>{{:~myFunction(~myConvert('param1'))}}</div>

Related

How do I interpolate variable string in pug?

In the following, myVar contains the string "Today, it's the ${date}".
Furthermore, there is an variable with the name date that contains "1st of October". I expect the following pug syntax to replace the literal ${date} with the date variable content.
span!= myVar
Unfortunately, the example results in
<span>Today, it's the ${date}</span>
Expected result:
<span>Today, it's the 1st of October.</span>
Best regards,
Benedikt
Yes, exactly as #omgninjas pointed out, it is called interpolation and preceded by # in Pug.
However you can't always use it (eg. inside a string). Here are some examples:
sensor is a variable passed by the controller to the view.
Normal interpolation. Works as expected:
<div id=#{sensor} style="width:90%;height:250px;"></div>
Inside a string with Template Literals (don't use these with user supplied values!):
img(src=`/images/${sensor}.png`, style="width:20%")
Inside a string used to denote a function call. Note that you cannot use the ` symbol (back tick aka grave accent used in template literals) with function calls because you would have to ecompass the entire function call . This results in a string which is not going to be executed. You need to use string concatenation.
body(onload="initTemp('"+ sensor +"')")
Here is the official documentation for Pug interpolation:
https://pugjs.org/language/interpolation.html
Hope this helps. Corrections and suggestions always welcome!
To render variables directly in a string in a Pug template, you can use the typical ES6 interpolation. Example (assuming pageTitle is in scope, and passed as template context):
- var pageTitle = `Google | ${pageTitle}`;
Pug interpolates with a hash. #{interpolation}

Escaping string parameters in Django templates for a JavaScript function

I have a JavaScript function which returns a set of objects:
return Func("{{id}}", "{{name}}")
I have a problem with passing strings containing quotes, for instance "Dr.Seuss' "ABC""BOOk"" is invalid syntax.
I tried {{name|safe}} but to no avail. Any solution for this?
If I'm right in assuming that's a JavaScript function call you're trying to interpolate Django templating into, try the escapejs filter instead of safe.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs

JavaScript: how to pass object value from one function to another

I am using code lines like the following in order to fetch data from an intranet website:
util.setProp(obj, "firstNameOld", $(msg).find('#fname_a').text());
Now I have another function in the same file where I want to use the above again, resp. the value of that object - currently I am hard-coding this ('Test') for test purposes:
util.setProp(obj, "firstNameNew", 'Test');
How can I pass the value from the firstNameOld object in one function to the firstNameNew object in another function ? If a solution with global variables is better here than this would work as well.
Many thanks for any help with this, Tim.
I've never used the framework that includes util But I imagine that if there is a setProp() then there has to be a getProp() or something similar.
If so, you could do something like
util.setProp(obj, "firstNameNew", util.getProp(obj, "firstNameOld"));
This also relies on the assumption that you want to copy from two properties in the same object.
If not, then pass the desired source object in the getProp() call.
My guess is that functions (or properties) are called "firstNameOld" and "firstNameNew", so the first time you get it from selector, second time you want to do the same.
Try to use the local variable like that:
var text = $(msg).find('#fname_a').text();
//
util.setProp(obj, "firstNameOld", text);
//
util.setProp(obj, "firstNameNew", text);

Passing variable length argument list to AngularJS directive

I have a directive I'm using for disabling buttons while I'm doing some behind the scenes work (to avoid getting double submits): http://jsfiddle.net/7nA3S/6/
I would like to be able to extend this so that the directive attribute can accept functions with arbitrary length argument lists.
I know the usual angular way is to just assign the values you need to other attributes on the element, but I'm wondering if there's a good way to generalize this. i could maybe do something like
<button my-submit='someFunction' args="arg1, arg2, arg3, ...">No Evals<button>
and then split up the args string, but maybe there's a less cruddy way?
Maybe something like this: http://jsfiddle.net/7nA3S/7/ .
You can use ng.$parse to evaluate the function with the arguments you have provided against the given $scope.
You then can get the arguments passed to the function from the arguments array inside the $scope.myAsyncSubmit function.

How could I add new property into a JSON string in JavaScript?

By JSON text, I mean the return value of JSON.stringify. I know how to do this with JSON object, but I couldn't figure out how to do this with JSON text (add new attribute/element, say "sn":"1" to JSON text, but its structure is kept and I don't need to stringify it again), can anyone help me?
Thanks!
I don't know why you'd want to do this - why not just add the property before you stringify it?
But if you must, given a string that contains JSON:
var myJSON = '{"prop1":"val1","prop2":"val2"}';
You can easily add a property to the beginning by doing this:
myJSON = '{' + '"sn":"1",' + myJSON.substr(1);
Or add it to the end:
myJSON = myJSON.replace(/}$/, ',"sn":"1"' + '}');
Or use whatever other combination of String manipulation functions takes your fancy...
If you want to add the new property in a specific place within the string, say inside a nested object or array or something, well, again some kind of regex or combination of .indexOf() and .substr() or something could do it, but really I think it's nuts to approach it this way.
Obviously the above code can be wrapped up in a function, and '"sn":"1"' can be replaced with a parameter or variable name or whatever - but why?
Note also that I've assumed above that there will be at least one existing property and inserted a comma accordingly- up to you to make that smarter if you want to allow for empty objects.
P.S. There aren't "JSON strings" and "JSON objects": all JSON is a string. In JavaScript one way of creating objects is with the object literal syntax that inspired JSON, but there's no such thing as a JSON object.
It makes no sense to do it the way you're suggesting... just turn it back into an Object, add your field and stringify it again! Or am I missing something?
You're going to have to parse it somehow. The most straightforward way is probably un-stringifying it to object/array/literal data. But if you don't want to do that, you could either use regular expressions, or methods of the String object like substr to manipulate the string directly.

Categories