Passing javascript objects as function arguments - json

I want to pass a javascript object (JSON) as an argument to another function. But i am getting following error:
missing ] after element list
the function is called on onclick event of href like
"<a href='javascript:void(0);' onclick='javascript:openTab("+ sTab +");'>"+ sTab['SavedTab']['title'] +"</a><br/>";
When i pass whole value : sTab['SavedTab']['title'] , it works fine but i want to pass whole object, not just single value out of it.
Please help me out.
Thanks.

This is because 'javascript:openTab("+ sTab +");', here sTab is a collection and the script cannot do anything for this collection. You would probably have to pass an index as "sTab['SavedTab']['title']"

I believe if your get sTab not as parameter but as variable inside your js function it will work. 'javascript:openTab();'
javascript:openTab(){
//sTab as global variable will be accessible here
}

Related

Arbitrarily run method during ngFor loop (Angular 5)

I have an angular page, where, during an *ngFor loop, I want to update a variable, then write it to the HTML during each iteration of the loop.
Like so:
HTML:
<table *ngFor="let data of Dataset">
somehowRunThis(data)
<div>{{methodResult}}</div>
</table>
TS:
...
methodResult: any;
...
somehowRunThis(data): {
let a;
...
this.methodResult = a;
}
etc etc.
Is there any way this can work? Attempting to add a method that returns within the curly brackets seems to not work, and there appears to be no effective way to run arbitrary methods from the HTML in Angular.
Thank you for any assistance you can provide.
Is there any particular reason why you want to trigger this update in HTML?
Depending on your needs you can use pipe (https://angular.io/guide/pipes) or transform the data to desired format in your component.
I would say it's not a good idea to have a method with side-call effects invoked in HTML.
There are a lot of ways to do this. A general advice: sometimes we are looking for an answer in the wrong places, be open :)
Instead of forcing ngFor, just run a simple array.map on your data before sending it to the template.
displayData = this.data.map(el => this.somehowRunThis(el))
this way you'll avoid having terrible performance.
If you don't care and still want to do this thing for some reason you can make your function return it and actually call in template:
{{ myFunctionReturnsText() }}
This is a bad idea because the function calls will run on each change detection so something like Pipes/Directives will be better.

Asp.Net Core 2.1.0-preview1-final: #Html.ActionLink() is not working for string.Format()

<div data-collapse class="left-justify" id="requirements">
#Html.Raw(string.Format(#_stringLocalizer["RegisterNoticeMessage"], #Html.ActionLink(#_stringLocalizer["RegisterLinkDisplayName"], "Register")))
</div>
In this piece of code, #Html.ActionLink() is returning Microsoft.AspNetCore.Mvc.Rendering.TagBuilder instead of returning anchor element containing URL path to the specified action.
What is the right way to use #Html.ActionLink() in string.Format(). Or, do I missing anything, here?
The helper method Html.ActionLink always returns a TagBuilder object. When you pass such an object into a string parameter, the ToString() method will be called, resulting in your observed output (the class name: "Microsoft.AspNetCore.Mvc.Rendering.TagBuilder").
It seems to me you are trying to create a hyperlink in a rather weird way. Have you tried using the Url.Action helper method? This method returns a plain old string, ready to plug into any href attribute.
E.g. this code would be equivalent of what you're trying to achieve:
#Html.Raw(
string.Format(_stringLocalizer["RegisterNoticeMessage"],
"" + _stringLocalizer["RegisterLinkDisplayName"] + "")
)
Sidenotes:
It is possible get the string value of a TagBuilder, as illustrated in this post.
No need to repeat # when you're already working in Razor/C# context.
Be extremely careful when using Html.Raw as it might result in XSS vulnerabilities.

Polymer callback as an attribute

Not so long ago I noticed that iron-ajax uses an attribut on-response="callbackFn" to execute once a response is received.
My question is simply how does this work?
I have a similar situation where I would like create a component that is able to take a callback as an attribute but I can't figure out how to do it.
Two things I noticed
When triggered the function is correctly bound to the Polymer element from which it came.
"callbackFn" is the name of a function. Not a polymer binding.
So I assumed iron-ajax must be doing some trickery in the background to bind the given function referenced by function name. When I looked at the source code though I don't see any handling of the 'on-response' attribute. none, zilch, nada. what gives?
I can see that there is a _boundedHandleResponse but that property does not seem to be bound to the on-response function at any point.
source
_boundedHandleResponse is a function which is value depends upon the _handleResponse function.
_boundHandleResponse: {
type: Function,
value: function () {
return this._handleResponse.bind(this);
}
}
So, if you check the _handleResponse function there is a 'response' event which is dispatched using the code :
this.fire('response', request, {
bubbles: this.bubbles,
composed: true
});
In polymer element we add event listeners using on-event annotations. For example on-tap, on-click. So, every time response is dispatched it will call the function defined as on on-response="callbackFn" which will call the "callbackFn" function.

How can I customize the filter function for SelectOneMenu

I tried to find on Primefaces Documentation but I have not found how can I customize the filter function for SelectOneMenu.
I add filterMatchMode="custom" filterFunction="#{mainRandevuBean.ilFilter()}"
But I don't know how can I write bean filterFunction.
The filter is a javascript (client-side) function. It all IS in the PrimeFaces documentation, which you should always look into first, carefully, thouroughly.
So use filterFunction="myFilter"
and create a javascript function like
function myFilter(itemLabel, filterValue) {
// return true if this label matches, false otherwise
}
Just as a sidenote: primefaces documentation doesn't say anything semantically about the parameters. It also does not mention where the label comes from (in fact, the docs mention "the item value" which is not very clear).
In fact I used the JavaScript function to debug this in order to figure out what was provided by default as a label.
function filterList(label, filter){
alert("label="+label+" and filter="+filter);
return false;
}
At first I thought it would be anything like the text inside the HTML generated for each list item. But when debugging it I saw the alert said that the label was something like my.package.SomeValueObject#123456 (which is obvously the Java object toString on each item in the list).
You need to define the itemLabel property on the selectItems which is inside the selectManyMenu to generate a proper text value used by the standard filtering mechanisme. As far as I could figure out that is the only reason why you have to put itemLabel there. In the documentation itemLabel is specified before explaining filtering which is confusing.
And as far as I know the itemValue defaults anyhow to just the object value, so I believe following from the documentation is redundant.
itemValue="#{player}"
Hope it helps anyone :.)
I resolve this problem with autocomplete component. Primefaces autocomplete component with dropdown="true" property works like one menu.

error called to undifined method when using pop()

I'm trying to add the pop function to a button in flex 4. But I keep getting an error call to undefined method when I try to use it, but it's a built in function isn't it? I posted my code below, any ideas?
<s:Button x="405" y="410" label="Undo last" width="85" click="data.pop()" id="undo"/>
data.pop() most likely won't work unless you are using your button as item renderer or item editor in a DataGrid.
In case you defined a variable of type Array called data try to rename it to something else. data is used by the framework itself.