Backbone toJSON not rendering - json

when I use Backbone toJSON method of the model like this:
this.$el.html(this.model.toJSON());
It doesn't render model into view root element ( more than one attribute ).
But when I get one property from the model, like this;
this.$el.html(this.model.get("city"));
It is rendered properly.
Also, when I use template in first case (toJSON) - it is rendered fine.
this.$el.html(this.template(this.model.toJSON());
Why is that ?
Thanks

this.$el.html(this.model.toJSON());
You're using the html method of jQuery, which expects a string (or a DOM element, or a jQuery element), to display a JSON object.
this.$el.html(this.template(this.model.toJSON());
Here you're using a template method which, I assume, is taking a JSON object to evaluate a template that will return you a string. The htmlmethod receives this string and displays it.
this.$el.html(JSON.stringify(this.model.toJSON()));
This would display the result of this.model.toJSON() (but won't do the same as using your template method).

So, basically this.template will be (in most of the cases) a compiled version of the html template which you have for the view.
It will have placeholders in it, and will take parameters with the same key as placeholders in the template. For example (Handlebars templates),
<section id="{{id}}">
<header>{{header_text}}</header>
</section>
Considering the above code as a template, when you compile and store it in this.template, it returns a function, which takes a json object as a parameter, so now this.template is a function.
You can call it like below,
var html_text = this.template({
id : "main_content",
header_text : "Hi Welcome !!"
});
this.$el.html(html_text);
After the execution, el's contents will be
<section id="main_content">
<header>Hi Welcome !!</header>
</section>
So when you do this.$el.html(this.template(this.model.toJSON());, it actually generates the required json parameter for the this.template method for you, hence works fine.
And as Loamhoof said, in this.$el.html(this.model.get("city")); you use the html method which will set the html content of the el based on the property value of the model.

Related

Semantic Mediawiki and properties in template

I have the following construction in the template to check whether attributes are full
{{#if:{{{typeip|}}}|'''Type IP:'''{{{typeip}}}|<big>'''Type IP:'''</big><span style="color: red">not filled in</span>}}
I want the typyap attribute to be a property so that I can find it by semantic search or by using a query via the #ask function.
I tried the following construction
{{#if:[[typeip::{{{typeip|}}}]]|'''Type IP:'''{{{typeip}}}|<big>'''Type IP:'''</big><span style="color: red">not filled in</span>}}
But it doesn't work. The #asc function returns an empty request and the type ip property page is empty. Can you tell me what I'm doing wrong? I performed reindexing using a service script rebuildData.php But that doesn't help either. I tried to insert the property in various other places in the template, but it doesn't work either. The template is filled in using the form.
Thank you in advance!
Of course, it doesn't work. You invoke the {{#if:}} parser function incorrectly.
Its parametres are:
the condition,
the value returned if the first parametre does not evaluate to an empty string or whitespace,
the value returned if the first parametre is empty.
Therefore the semantic annotation should be in the second parametre, not the first:
{{#if:{{{typeip|}}}
| '''Type IP:''' [[typeip::{{{typeip}}}]]
| <big>'''Type IP:'''</big><span style="color: red">not filled in</span>
}}
{{{typeip}}} should be bare text without any wiki formatting. If you must pass wikitext to it to convert wikilinks into semantic annotations setting properties of the type Page, the simplest way will be as follows:
Install Scribunto,
create Module:Inject with the following content:
return {
property = function (frame)
local wikitext, property = frame.args[1], frame.args[2]
local annotation = mw.ustring.gsub (
wikitext,
'%[%[([^|%]]+)(|[^%]]*)?%]%]',
'[[' .. property .. '::%1%2]]'
)
return annotation
end
}
put the following in your template:
{{#invoke:Inject|property|{{{typeip|}}}|typeip}}
The template will convert any wikilinks in {{{typeip}}} like [[A]] or [[B|C]] into semantic annotations like [[typeip::A]] or [[typeip::B|C]].

Angular data binding sending strings?

I'm new to angular, and I would like to know if there's is a way to send a string to the Html file with a variable inside?
test.ts
test: string = "Display this {{testText}}";
testText: string = "Success";
test.html
<p>{{test}}</p>
What I want to achieve is that it displays this: Display this Success.
I'm just curious if this is possible, perhaps I can retrieve from an API chunks of HTML string and display them like that.
**
It is basic Javascript string operation. For this, there is nothing special with Angular at your TypeScript file.
Without handling updates on test
On Typescript file you have two options to merge strings:
First Way:
testText: string = "Success";
test: string = `Display this ${this.testText}`;
Second Way:
testText: string = "Success";
test: string = "Display this " + this.testText;
Of course you can see a problem with both of them. What will happen when you update your test? Based on these ways, the testText just initializing when the component instance is created, so if you want to fetch changes on your test variable you should use the way from one of following
**
First Way:
test.html
<p>Display is {{testText}}</p>
<p>{{'Display is ' + testText}}
Socond Way:
Specifically you can create a custom Pipe. You should check documentation about how are them work. For only this case you don't need to use this way. Pipes are generally for more generic or more complex operations.
Third way:
(more bad than others. Because change detector of Angular will not understand when your content should update the paragraph. You should use others.)
test.ts
getTestText() { return 'Display is ' + this.testText }
test.html
<p>{{ getTestText() }}</p>
**
Binding Dynamic Html Content
For binding any dynamic HTML template you need to use innerHTML attribute like
<div [innerHTML]="htmlVariable"></div>
but this is not a trusted way because there is nothing to check is the html is trusted or is it valid etc. Or if the html contains the selector of any component, it won 't render as expected. You should use more complex ways to do it.

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.

Scala Play template vararg HtmlContent

I have a generic template in play 2.6, that I want to pass in a variable amount of HtmlContents. I've defined the template like this (including the implicit parameter I have in case that changes anything):
#(foo: String)(content: Html*)(implicit bar: Bar)
On the template side, this works fine-- I can dissect content with for and render it as I want. However, I haven't been able to figure out a clean way to invoke the variable arguments from the underlying template.
e.g, I have a view named "Baz":
#(something: String)(implicit bar: Bar)
In it, I try to invoke the template with multiple Html arguments. I've tried the following:
#template("fooString"){{123},{abc}}
and
#template("fooString")({123}, {abc})
and
#template("fooString"){{123}, {abc}})
And various other permutations, but inside of an enclosing bracket it seems to interpret everything literally as a single parameter in the HtmlContent vararg.
However, this ended up working as I intended, passing in multiple HtmlContents:
#template("fooString")(Html("123"), Html("abc"))
So that works, and I can use a triple-quoted interpolated string for a large Html block-- but it seems like there should be a cleaner way to do this, and the string interpolation is dangerous as it doesn't do html escaping.
Is there a way to do this using the { enclosed syntax? I'd like to understand more what is actually happening on an underlying level, and how play parses and generates HtmlContent in brackets.
So consider you have below template
// main.scala.html
#(title: String)(contents: Html*)
There are different ways you can call this template
Option #1
This is what you already posted in the question
#main("This is a title")(Html("abc"), Html("123"))
Options #2
#html1 = {
Hello
}
#html2 = {
<div>Tarun</div>
}
#main("This is a title")(html1, html2)
Option #3
#main("This is a title")(Html(<div>Tarun
</div>.toString), Html(<div>
Lalwani
</div>.toString))
Options #4
This is not exactly same option, but needs change in Template signature itself
#(title: String)(contents: List[String])
And then calling it like below
#main("This is a title")(List(
"""<div>
Tarun
</div>
""", """Hello"""))
Option #5
This requires code files and was already answered on another SO thread
Paul Draper's answer on Why doesn't this pass multiple HTML parameters to template

reading cookie value in play framewrok 2.2 scala template

I am trying to read a cookie value inside the play framework template (not inside a controller). I am trying the following which is not working:
# val cookieVal = request.cookies.get('PLAY_SESSION').value
Any suggestions to fix this will be greatly appreciated. The reason why I am trying this is to change how the page gets rendered based on a cookie value.
In templates you define vals as follows:
#defining(request.cookies.get('PLAY_SESSION').value) { theValue =>
<div>Hello #theValue</div>
}
I personally prefer to read the cookies in the controller and pass them to the template if needed.
suppose PLAY_SESSION stored "37f0983881ba00636868b42234a360d466fb944c-block_status=0&userId=159313257462171"
and you have to render on the basis of the value of block_status.
then in this case you can get its value by
session.get("block_status").get
to use it in template you have to import#implicit session:play.api.mvc.Session at your template.
now you can easily get values at template by #session.get("block_status").get
request.cookies.get("Org").get.value