Separate files for functions outside classes - function

It's a good practice to separate each class from the main code using
headers and sources. But what about functions? Let's say, I have a
function I would like to use across multiple classes and I don't
want to include this function as a method of a class.
If I decide to create a separate file for this function(s), should I
put everything inside .h or should I do as I do with classes
(separate .h and .cpp)?

Yes, whether it's a class or not it's still good practice to separate the declaration / signature (in the header file) from the definition / implementation (in the cpp file).
The code that calls the function does not need to know about how the function actually works - just how to call it.
This separation can avoid circular references that can sometimes otherwise occur. It avoids the compiler having to re-parse the definition every time the declaration is included.
Basically the reasons for separation between header and cpp are much the same for class and non-class functions.
However if you're using templates you will need to include the definition not just the declaration ( as you would with template classes ).
I'd suggest you put the functions in a namespace even if you don't put them in a class.

Related

Move 2sxc <script> to external file when it has razor content

I'm trying to make my CSP without unsafe inline.
Since I have to manually check every file from every app, I may as well move the scripts to external files instead of creating a million word CSP entry in the web.config by adding hashes or nounces.
This seems easy enough for client side content, but many templates have razor code in then such as:
<script>
alert(#myVar);
</script>
How can I move this to external?
So in general if you JS needs some input parameters you must of course put them somewhere, and only the razor will know what they are.
The simplest way is still to just have the initial call use the variables - like in your example above. If you have security concerns, doing type-checking in razor should eliminate that for you.
For example, if you do #((int)thing.property) than it simply cannot inject any unexpected payload.
If for some reason you really, really don't want this you can use a attribute-json convention, like
<div class="myGallery" init='{"files": 17}'> gallery contents </div>
and pick it up from the js you created. but this is quite a bit of work, so I would recommend the simpler way.

Angular 2+ multiple HTML template for one component

I have the following scenario. I am writing a complex component that is using three-js:
The component manages complex mouse interactions and updates other elements in the DOM using two-ways data binding: variables, JSON objects, mouse interactions, etc.
I start using the component in few part of my application but I needed to substantially modified the threed-viewer.html so I made a copy of the whole component ending up having duplicates that are hard to maintain.
All flavours of the component share 80% of the javascript code and bindings but they have substantial UI differences. So I had the though of create different 3 basic component (minimal javascript code) that I can inject into the threed-viewer.html using a selector and a variable to decide which template to load:
this does not compile as the html files have all the variables and bindings from the original components but they are not present in the typescript files.
Another solution could be to have a single html managed via ngIf but it will result in a long, messy, difficult to manage file. Is this the only option I have in Angular. Any other idea?
Thank you.
You can have a shared service and then add three different components but js code won't be duplicated as it will be in the service.
Use two way bindings in all 3 components using the service variables, functions and objects
I have did long time ago - the pages and article editor. Two different templates, same code.
I used articles component as "parent", and extended it to news editor.
Looks like this:
#Component({template: 'blah blah blah'}) export class parent {}
#Component({template: 'blah2 blah2 blah2'}) export class child extends parent {}
Hopefully this is the solution you were looking for.
Two different components, two templates, one code.
Of course, you can have the "parent" abstract class for both where you can save all methods you need.
It's OOP baby ;)

phpDoc for object's properties

I've got file template.php, that is included in CBitrixComponentTemplate's method.
CBitrixComponentTemplate has property $this->__component, that is dynamicly filled with object EtmOrderAdd. EtmOrderAdd has property objValidator.
Now, when I am writing the file template.php, I need all these properties to be understood by phpDoc.
Here's the picture, illustrating my idea :
How should I write it?
Bitrix has almost no phpDoc for internal methods and functions. Also it has a plenty of legacy code, that won't show correctly with PhpStorm's code completion.
However, there are several things you can try to improve the situation:
Add bxApiDocs files as an External Library to PhpStorm.
This will add autocomplete for the internal bitrix methods
It seems like you've defined custom component class since $template->__component usually contains CBitrixComponent object that doesn't have objValidator property. So you need to add #property inside your class.php file of your component.

include/import separate AS3 file

I am trying to just include a .as file in my flash application. I'm sure it's not that difficult and I'm just getting something slightly wrong, but at the top of my code I always put:
include {"Le Bot src/Skill.as";}
OR
import {"Le Bot src/Skill.as";}
to try and include a separate .as file. But, when I use include it comes up with the error:
expecting stringliteral before left brace
and when I use 'import' it comes up with the errors:
expecting identifier before left brace
expecting semicolon before left brace
I am just a beginner to AS3, is there any way to fix this problem?
Btw, if I remove the two braces on either side, then it comes up with another error
import locationOfASFile.Skill;
e.g. if your Skill.as file was located inside a directory named 'LeBotsrc' which was inside a directory named 'com'
import com.LeBotsrc.Skill;
Also your as package needs to reflect it's location. So inside Skill.as you would have
package com.LeBotsrc {
The include directive is not much used in AS3, since organizing code using packages and classes is easy. However, inlcude has a weird thing. No ending semicolon.
Try this:
include "Le Bot src/Skill.as"
So to sum it up:
include is not the same as import.
include does not use curly braces.
include MUST NOT have a semicolon at the end of the line.
inlcude must be on one line in the format of:
include "pathto/myfile.as"
Using include will add the actionscript in the specified file to the timeline where the include is used.
You could also accomplish the same thing by setting a document class that extends the main timeline or a symbol in the library. Doing so adds the complexity of needing to work with classes, but classes pack more than enough power and flexibility to make up for their additional complexity.
For example, using an include, you cannot specify a function or variable to be public or private. They are all public. Using classes makes it easier to make your code Object Oriented, which is a great way to make your projects more programmer friendly.
That Le Bot src\Skills.as should first be accessible by a correct relative path (package\name\class.as) from your project folder, then you do import package.name.class, placing your values instead of placeholders here. Say, you look into that Skill.as and find out:
package foo.bar {
import baz.*
...
public class Skill {
This means the AS file should be located at foo\bar\Skill.as where foo folder should be in the folder where is your FLA. In case of FlashDevelop, it should be inside src foder from *.as3proj file. Place it there, and add:
import foo.bar.Skill;
to whatever file or timeline you want to refer this class from.

Multiple views for 1 controller - Play Framework [duplicate]

If i want to have a common piece of UI across multiple pages, such as a menu, what is the recommended way to do this?
It would contain both template code and a back-end controller (similar to "snippets" in the LiftWeb framework).
I am aware that there is a menu module for Play, but I'm more interested in how this would be achieved in general.
There are two ways to include common view code into the Play Framework.
You can use the #{include} tag or the #{extends} tag.
The extends tag, as the name suggests, extends from a parent view. The extends tag is used by default in the skeleton code set up by Play when you create a new application. It extends the main.html. You add your code here.
The includes tag, allows you to inject a common piece of view code into your templates at a specified point. This works in much the same was a php include/require, or jsp includes work.
The problem will come when your template code also requires data or logic from the model (via the controller). If this is the case, then you will need to use the #Before or #With notation in your controller to ensure that the common piece of controller code is executed each time. You can add any data to the renderArgs list, so that it is available for use within the view.
A simple example of using renderArgs would be.
#Before
private static void commonData() {
// do your logic here
renderArgs.put("menu", menu);
renderArgs.put("selected", selectedMenuItem);
}
the values you have put into renderArgs (menu and selected in the example) will be available just in the same way as if you passed them into the render method.