How to add more information to first html page sent by meteor server - meteor

Is there anyway we can add data like in php echo "something" in the first html page. I want to know the server's timestamp to format a document created time like 2 hours ago, the document already has a property createdTime. When I use Meteor.Collection.find, I cannot add the server time by using transform.
I can use Meteor.method but I may have to format time before the result arrives.
Thank you.

Well, after digging around the code, here is the answer.
You can use the global variable __meteor_runtime_config__ to add more information to the first downloaded html file. In my case, in a server side javascript file, I add __meteor_runtime_config__.now = new Date().getTime() and this value will be available on the client side

the __meteor_runtime_config__ approach is run-once; that is, only changes made at package load time (not Meteor.startup()) are taken into account, and then the __meteor_runtime_config__ snippet is frozen.
To pass run-time (per-page) metadata to the page, it looks like the only option is to set a custom tag on the <html> element using the (public, but undocumented) WebApp.addHtmlAttributeHook API.

Related

Here API | MultiValueQueryParameter for Via Points

I'm using the latest version of the here javascript sdk 3.1.32.0
When I use H.service.Url.MultiValueQueryParameter for my via points like
{
...,
via: new H.service.Url.MultiValueQueryParameter(['50.1234,8.7654', '51.2234,9.1123']);
}
I see in the URL params of my request this: &via=%5Bobject%20Object%5D
Someone an idea why this happens?
Thanks in advance!
When you use "via" parameter, you define a list of via waypoints. A via waypoint is not a native datatype recongnized by JavaScript, therefore, you will see in the URL params the word object refering to a particular data structure, in this case the via Waypoint composed mainly by Latitude, Longitude.
Regards.
This took a while to figure out the actual issue with the malformed url params. I was dynamically loading the here maps service script on mount of a component. Accidentally this happened in some cases twice. In both cases H was globally available and everything worked like expected. BUT when the script was loaded twice H.service.Url.MultiValueQueryParameter didn't return the correct params. It basically encoded them twice, or tried to.
The other side issue was that https://www.npmjs.com/package/#types/heremaps is outdated and doesn't cover MultiValueQueryParameter. So I had to remove the types and use my own. Else I probably would have realized the issue earlier.

How to update mark a form dirty after a server side modification

I don't understand how to make the Client ui aware of a server side change in a document.
My use case is the following : I'm adding a button to my form, that calls a custom document method :
In MyDocType.py, I have :
class MyDocType(Document):
#frappe.whitelist()
def change_some_value_in_doc(self):
self.the_field_i_want_to_change = 50
It work in the sense that the_field_i_want_to_change is immediately updated to the new value.
However, If I hit Save, I get the message "No changes in document". I managed to determine that this is because on the JS side, the form is not marked as "dirty", but despite reading the documentation over and over, I cannot find the right way.
I tried with self.notify_update() which seems to have no effect.
Is it a bug ? Or am I misunderstanding how this is supposed to work ?
I'm using frappe v 14.0.0-beta.3
[EDIT]
Actually, I realized that doing
class MyDocType(Document):
#frappe.whitelist()
def change_some_value_in_doc(self):
self.the_field_i_want_to_change = 50
self.save()
Does work in the sense that it save the new value, but it's not exactly what I'm looking for, because my purpose is actually to pre-fill some data with the custom method and leave to the user to complete before saving...
Any help to get me in the right direction would be very appreciated.
To pre-fill data in your form - that you want the user to confirm before saving to the database, you should use client-side scripting (in JS). Changing the value on the server is not going to help you since you want the user to get pre-filled values before inserting a record in the database.

Meteor how to save templates in mongo

I want to give my users the possibility to create document templates (contracts, emails, etc.)
The best option I figured out would be to store these document templates in mongo (maybe I'm wrong...)
I've been searching for a couple of hours now but I can't figure out how to render these document template with their data context.
Example:
Template stored in Mongo: "Dear {{firstname}}"
data context: {firstname: "Tom"}
On Tom's website, He should read: "Dear Tom"
How can I do this?
EDIT
After some researches, I discovered a package called spacebars-compiler that brings the option to compile to the client:
meteor add spacebars-compiler
I then tried something like this:
Template.doctypesList.rendered = ->
content = "<div>" + this.data.content + "</div>"
template = Spacebars.compile content
rendered = UI.dynamic(template,{name:"nicolas"})
UI.insert(rendered, $(this).closest(".widget-body"))
but it doesn't work.
the template gets compiled but then, I don't know how to interpret it with its data context and to send it back to the web page.
EDIT 2
I'm getting closer thanks to Tom.
This is what I did:
Template.doctypesList.rendered = ->
content = this.data.content
console.log content
templateName = "template_#{this.data._id}"
Template.__define__(templateName, () -> content)
rendered = UI.renderWithData(eval("Template.#{templateName}"),{name:"nicolas"})
UI.insert(rendered, $("#content_" + this.data._id).get(0))
This works excepted the fact that the name is not injected into the template. UI.renderWithData renders the template but without the data context...
The thing your are missing is the call to (undocumented!) Template.__define__ which requires the template name (pick something unique and clever) as the first argument and the render function which you get from your space bars compiler. When it is done you can use {{> UI.dynamic}} as #Slava suggested.
There is also another way to do it, by using UI.Component API, but I guess it's pretty unstable at the moment, so maybe I will skip this, at least for now.
Use UI.dynamic: https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/
It is fairly new and didn't make its way to docs for some reason.
There are few ways to achieve what you want, but I would do it like this:
You're probably already using underscore.js, if not Meteor has core package for it.
You could use underscore templates (http://underscorejs.org/#template) like this:
var templateString = 'Dear <%= firstname %>'
and later compile it using
_.template(templateString, {firstname: "Tom"})
to get Dear Tom.
Of course you can store templateString in MongoDB in the meantime.
You can set delimiters to whatever you want, <%= %> is just the default.
Compiled template is essentially htmljs notation Meteor uses (or so I suppose) and it uses Template.template_name.lookup to render correct data. Check in console if Template.template_name.lookup("data_helper")() returns the correct data.
I recently had to solve this exact (or similar) problem of compiling templates client side. You need to make sure the order of things is like this:
Compiled template is present on client
Template data is present (verify with Template.template_name.lookup("data_name")() )
Render the template on page now
To compile the template, as #apendua have suggested, use (this is how I use it and it works for me)
Template.__define__(name, eval(Spacebars.compile(
newHtml, {
isTemplate: true,
sourceName: 'Template "' + name + '"'
}
)));
After this you need to make sure the data you want to render in template is available before you actually render the template on page. This is what I use for rendering template on page:
UI.DomRange.insert(UI.render(Template.template_name).dom, document.body);
Although my use case for rendering templates client side is somewhat different (my task was to live update the changed template overriding meteor's hot code push), but this worked best among different methods of rendering the template.
You can check my very early stage package which does this here: https://github.com/channikhabra/meteor-live-update/blob/master/js/live-update.js
I am fairly new to real-world programming so my code might be ugly, but may be it'll give you some pointers to solve your problem. (If you find me doing something stupid in there, or see something which is better done some other way, please feel free to drop a comment. That's the only way I get feedback for improvement as I am new and essentially code alone sitting in my dark corner).

Open direct links to AX-objects or datasets from external application

Is there a way to open a specified document, eg "production order 123" or form, eg "purchase orders" in Ax2012 from an external application directly?
In detail, I'm looking for something similiar like AXPath, but this doesn't work with versions greater then 2009.
Is there any ( maybe included ) way to achieve this?
There is! It's using AX's drilldown functionality which uses AxHLink.exe to handle dynamics:// URLs, which are passed to the Classes\SysStartupCmd function. You could also create some custom code there if you wanted to launch the AX client executable directly.
My question I asked some while back should have a great deal of useful information in it here:
What handles dynamics:// URLs?
Some more can be found: http://technet.microsoft.com/en-us/library/aa834337.aspx
EDIT:
It sounds like you are confused or the posts weren't clear enough. I think you have 3 basic options.
Dynamics:// URLs are handled by AxHLink.exe and they only seem to handle drilldown, viewalert, and viewalertrule. So if you want to use Dynamics:// URLs, you will need to hi-jack those somehow. There is a pastbin from Jan in that other stack post.
Create a custom URI handler and event poller (lot of work) see http://axcoder.blogspot.dk/2010/10/how-to-open-form-in-running-ax-from.html
Extend SysStartupCmd and then instead of using Dynamics:// URLs, just call Ax32.exe -startupCmd directly and a parameter can be passed to your custom class.

Lua socket: why doesn't socket.http.request download entire html code of a page?

I want to download to a Lua string the entire html code of this web page. The problem is that the saved code skips some lines that are visible on Google Chrome when I use Inspect Elements command (right click on webpage, last option).
I need that lines because I want to use the informations written there, to be precise I want to catch the card prices.
The code used is simply:
-- loads the HTTP module and any libraries it requires
local http = require("socket.http")
-- Lua socket --
b, c, h = http.request("http://magiccards.info/roe/en/4.html")
print(b)
How can I solve it?
Probably some of the elements on the page are created after the main document loads, either by Javascript DOM operations, or AJAX queries which fill up the missing parts. You could look if these scripts are indeed downloaded and then run them locally using some embedded engine like V8.

Resources