I have my i18n in a JSON file and I need to call a function from a piece of string.
I'll give you an example:
"give_me_feedback": "Do you need something else? <button onclick=\"sendFeedback()\">Send us your feedback</button>"
I call this message in my component, just like that:
<p [innerHTML]="'give_me_feedback | translate'"></p>
Usually, when I put a class or a button inside the i18n JSON file works fine, but, in this case, where I am calling a function specifically, it is not working.
Do you know if it is possible to do that from a JSON file? Or maybe I can put some kind of identifier and then call it from the component? I know that it could work in javascript, but I am not getting a good results in Angular. I' am using Angular 14.1.1
Thank you in advance!
Related
I'm trying to create a very simple case: a controller action which renders a static JSON, from a template.
controller:
defmodule MyApp.TestController do
use Phoenix.Controller
def show(conn, _params) do
render(conn, "show.json")
end
end
view:
defmodule MyApp.TestView do
use MyApp.Web, :view
end
show.json.eex:
{
"message": "Hello, world!"
}
The problem is, I get the proper JSON response, but JSON-encoded:
"{\n \"message\": \"Hello, world!\"\n}"
Any idea why, and how to solve it?
/Edit:
I found out that I can work around the problem by renaming the template to something other than json (plus explicitly setting response type, of course), so obviously JSON templates are additionally encoded. But why, who would want such a thing?
After further investigation and talking with people on Phoenix Slack channel, I have a clearer picture about what's going on:
Phoenix is agnostic when it comes to deciding whether the content comes from a template or from a data structure in the view. render/2 from the controller happily takes anything and converts it to JSON.
I wrote up a more detailed blog post about this issue, along with several approaches in addressing it, and in my opinion every is a workaround for the inherent problem in Phoenix (which, admittedly, is far from critical).
Essentially, one should avoid executing Poison.encode function, which gets called from render_to_iostream function. You can do that either by not using .json as the template extension, by directly calling Phoenix.View.render from the controller, or by creating custom encoder and template engine to pass some metadata along with the data to be output.
I am working on play framework with SCALA as backend.
Json data is given to the front end from the controller.
I want to add HTML as value of some fields of json.
This HTML will be kept as a template and data will be added in this template at run time.
I think i should put unique names in the HTML template and then these names will be replaced by the data which i want to add at run time. Ultimately, this HTML will be added in the json response.
Is my approach right? If not, what is the best approach to add data in an HTML template,add this template in json response and send this combined response to the front-end for further use?
Is it a good practice to use string replacement to add data in an HTML template?
I think as long as you use Play, you can put your HTML templates into app/views package. Let's say you call your template mytemplate.scala.html
You can parameterize this view as any Play view.
In the place in your code where you generate your JSON response you can then call mytemplate(parameters) to get html generated, Play will do all the work here for you. Then using play.api.libs.json.JSON object's methods and related facilities you can convert this html to JSON.
So in your controller's code you will have something like Ok(JSON.toJson(mytemplate(parameters)))
This is of course a sketch, so you will need to elaborate and try.
I am getting the data from server in JSON and I am converting that to my viewModal. Now I am loading the a new view using return router.navigate('results'). But at the same time I want to pass the viewModel which I created from the JSON object to be passed to this view. By default the "results" viewModel is getting called but it is having blank values.
A small code snippet will be helpful as I am new to Durandal JS.
The best answer I have is to store that information in a separate module, like so:
storage.js
define(function(require) { return []; });
viewmodel.js
define(['storage'], function(storage) {
$.get('uri', function(data) {
data.forEach(function(obj) {
storage.push(obj);
});
});
});
This is not the most elegant solution. I'm really interested if there is a clean way to pass data from and to separate view models on activation, but so far I have found none.
Ended up finally something like this.
In the module where I want to navigate, I created an "Initialize" function which accepts a JSON object. Using this JSON object I initialized all properties in the viewModel.
From the page from where I have to navigate there I did a "require" on the module where I want to navigate next. Then I called the "Initialize" method and passed my JSON object.
After this I used router.navigate method to go that module.
This way when I navigated, I got all the values which I wanted to pass from one view to other. Hope this approach will help someone else.
The answer from Matthew is certainly similar to the way that I currently do it, once the data is held within your separate module (i have a path called modules/data/someDataStorage) you can use either events or a knockout observable to make the data update through to your view models.
In your case I would make updates to your shared module to store information on your request and then on the activation of your results module, go and get the data from that shared module.
http://www.playframework.com/documentation/2.1.x/JavaTodoList
Using the above tutorial as a reference, I have created an application which sends data from the model to view via the Application controller.
I have managed to display the model(Tasks) as a high chart. The code is here.
public static Result format(){
return ok(views.html.frmt.render("Visualize it",Task.all()));
}
This goes to this view page.
http://ideone.com/ycz9ko
Currently, I use scala templating inside the javascript code itself. Refer to lines 9-14 and lines 20-24.This unelegant style of doing things is not really optimal.
I want to be able to accomplish the above using Json instead.
public static Result jsonIt(){
List<Task> tasks = Task.all();
return ok(Json.toJson(tasks));
}
My Qns are how to send the JSON objects to a view template.
And how to parse it into a Highcharts format. Is there some standard procedure to do this ? Or else I have to write my own method to do this ?
It'll great if someone can show me a code snippet. Also I would prefer a post not using Ajax. I would just want to know how to do this first.
I also found this stackoverflow post useful.how to parse json into highcharts. However, it didnt answer the part about converting from Play format to Highcharts format.
Thanks in advance
You don't need to pass a json object to your template, instead you might do an ajax call from your client side javascript (your template) and get json response that you could use futher in javascript code to build a chart. For example :
You have some path that is bind to your controller jsonIt() like so /chartsdata/json
then using jquery shorthand for ajax request:
var chart_data = $.get('/chartsdata/json', function(data) {
return data;
});
now you can use a chart_data that is an array of objects where each object represents a Task, in your further javascript code to build a chart.
I'm using a Sinatra app to receive server requests and I want to dissect them in a separate class I call "request", but when I pass the request object the body gets dropped. Trying to read the request.body in the main class works but trying to read it in the new class generates a JSONparser octet error.
In the main Sinatra file, this test call generates the correct response:
puts JSON.parse request.body.read
after, I pass the request to the Request Class with the code below.
req=Request.new(request)
But in the Request class initialization def, the same "puts" code above generates the error:
JSON::ParserError - A JSON text must at least contain two octets!:
Both files include the JSON requirement.
A work around is fairly simple but I would prefer the more elegant solution if I could figure out why it is not working as I expect. Any thoughts are appreciated.
from my tests
the Request.new constructor doesn't seem to clone from Request object
request.clone works proper
you need to do the thorough object inspection if you need anything extreme