Undefined helpers fail silently - meteor

When i use something that is undefined in my templates I'd like to have it log an error instead of failing silently. is there a way?
I just spent 20 minutes trying to figure out why my helper wasn't being called and it was defined on the wrong template.

I use global helpers instead. Templates are accessible from the command line so that's why I end up doing this anyway. If I need custom helpers for a template I define them with Iron-Router on the data object of the route.
Also, to ease this behavior you could make a helper for your app (as in App.helper.return), containing, say _.isUndefined(helperReturnValue), to throw an error. But then of course, this is not that convenient for production unless you, somehow, set client-side the process.env.node_env variable of your server.

Related

global variable in optaplanner

In drools rule file how do I set the value of (or initialize) a global variable for optaplanner. My use case is following:
I want to declare a global java map that is constant and will not change during the execution. Every rule will access the map to check for a value, if value is in the map then rule will evaluate to false. The map is being generated before execution starts by accessing data in files/database.
This link https://issues.jboss.org/browse/PLANNER-94 is also requesting the access to global variable but this feature is rejected now.
How do i use the hack defined in this link: Setting global variables in working memory in Drools planner, from which object i should get CustomSolverPhaseCommand? [I am not able to comment on this post yet because i don't have enough reputation, sorry if it seems duplicate question].
I am creating SolverFactory from xml Resource and xml file contains the path to .drl file. Just like in .drl we can access object HardSoftScoreHolder scoreHolder, I want to access the map in the same way in 'then' part of rule.
can anyone please help?
Look at the examples that have a class that ends with Parameterization instead, such as ConferenceParametrization, this is probably a better alternative than globals.

Where to put a function required by multiple pages that also requires the session information?

I have this neat function defined in a .aspx.vb file, it requires the Session variable from the context to run. I guess I could just clone the function to the other .aspx.vb file where i need it too but is there like a higher place I can put it where it would be available to both page classes and I could still have the current session variables?
You can get the session from anywhere in your application without having to pass it or anything. Just fully qualify the session method.
HttpContext.Current.Session("MySessionVariable") = Something

Meteor autoform does not wait for element to exist in DOM

I'm adding a rendered template to the document via:
Blaze.renderWithData(Template.page, Session.get(toAdd[i]), document.getElementById('pages'))
This works great... except sometimes with autoform. I'm unsure what's triggering it to happen but some of my autoforms (that can exist inside Template.page, they're added to that template dynamically via the data passed in) show, while other throw a client error:
Uncaught TypeError: Cannot read property 'formValues' of undefined :: autoform-inputs.js:162
markChanged :: autoform-inputs.js:169
updateTrackedFieldValue :: afFieldInput.js:72
There's a timeout set to 0 in that afFieldInput.js on line 71. Upping this to 500 fixes the error above but is hacky and causes other issues.
The best I can surmise, autoform is attempting to get the form from the DOM by id before it exists in the DOM, probably it exists as a document fragment via Blaze, if I had to guess.
I can confirm that AutoForm.templateInstanceForForm is getting the correct form ID, but that document.getElementById(formId) is returning null.
The other possibility is that Blaze is, for some reason, failing to render the template but not warning me.
I was WAY off.
I had changed the schema, but was trying to open a doc with improper data for the new schema. Going to post this answer in case someone comes across the same error.
Also, don't pass in objects or functions all the way to the template that autoform doesn't expect, apparently. You can access them still, but remove them from the attr object by declaring attr as a function in the helper, returning this.attr sans any objects that don't need to be sent on to the template.

Can we use PHP inbuilt function inside Twig File in Symfony

Hi Can anyone Please let me know can we use php inbuilt function inside a twig file.if not then why.
What is the way then to access php inbuilt function inside a twig file.
Because in a application in 100 of time we need to check many conditions basis of php inbuilt function.I have tried in_array() function to check multiple vaslue selected in a multiple dropdown list but i am getting error Is_array() not defined.
Please help
Thanks
As #DonCallisto Said, There is some PHP equivalent function exists in twig not all. So you cant call a php function from twig template. You may have to use a existing equivalent or need to create one if not exists.
Why?
One of the main reason is SoC. Template is for presentation layer of your application. So twig made available tools(filter,functions, global variables) to do that.
Know the differences
Though you have date function in twig. its not the same date function you have in php. To achieve a similar functionality you may have to use same or different approach in twig then php. for instance you can achieve php's in_array functionality using the twig's Containment Operator
What is the way
Now come to last part of your question:
What is the way then to access php inbuilt function inside a twig file?
I think you already know the short answer from #DonCallisto. You can create your own extension. And define what function you needed. Or if you are crazy enough to access all php builtin function from your template, you can use this Extension. It will allow you to call any php functions by prefixed with php_. for example if you like to call in_array function, then you can call like php_in_array() from your template.
Happy coding!
Twig have some php builtin functions equivalent. For instance in_array() php function is in twig function. Check it out
If you don't find some builtin, you need to write your own twig exstension

Accessing other templates' instances

You can access the current template's instance by doing Template.instance(). But you often run into situations where you have to access other templates' instances as well. For example, if you use ReactiveVar, then you would want to get or set variables that are attached to other template instances.
I came across How to get the parent template instance (of the current template) but this is not complete.
Q1. How can we access any template's instance, not just the current template's
Q2. Is it against the Meteor way if I need to access other templates' instances?
you can try to set your template variable directly at the template level instead of inside the instance.
Template.example.myVariable = new ReactiveVar();
instead of
Template.example.onCreated(function (){
this.myVariable = new ReactiveVar();
});
The closest I got was to target the template by one of its elements (assume the template contains a form)
Blaze.getView($('form')[0]).templateInstance().someReactiveVar.set('changed')
If your target templates are in the same file, you can just define the reactive variable outside the template functions, at the beginning of the file. All templates in the file will access it.
If your target template is the parent template, (or any further parent template) you can access its data context using Template.parentData() the argument being the rank of the parent (default is 1). It seems that you know that already.
If you need to access a DOM element within a different template in the same page, you can use jQuery selectors.
I don't know any other way to reach another template instance (afaik, there is no Blaze.getTemplate(name) function.) The answer you are referring to seems to be the better you can get.
I think this is purely subjective, since in Meteor there are so many different ways of doing things, but I actually think Session is perfectly suited for sharing variables across several templates. People argue that Session is bad since it's global and can pollute the namespace. I would argue that it's up to the developer to keep their environment clean in any way that works for them. So for instance, this is bad:
Session.set('count', 23);
Session.set('last', new Date());
But this is better:
Session.set('notifications/count', 23);
Session.set('notificatinos/last', new Date());

Resources