Angular js directive dynamic template binding - templates

I have a template which looks like:
scope:{
localClickFunc: "&click",
myLocalDbClickFunc: "&dblclick"
} ...
<myButton ng-click="localClickFunc($event)" ng-doubleckick="myLocalDbClickFunc($event)"/>
there are many other events (mouseover etc)
my localClickFunc are binded in scope directive with controller functions(they could be binded with "=" for my case It doesn't matter).
The problem is that in the usage of this 'myButton' directive not all attributes are necessary .If I use it with all the other events will get registered and fired through angular to a noop function.
I can have as many as 1000 buttons on the screen .What would be the solution to this ?. Are there conditional templates ?

The ? in the scope binding definition makes the attribute optional.
However, function bindings (&) are always optional, as far as Angular is concerned. I.e. Angular will not complain if you do not specify a function in this binding and it will put a function in the scope of the directive anyway. So you cannot write if( scope.localClickFunc == null ) to check for its existence.
I would suggest to use the optional binding =? to specify the callbacks. This way you will be able to check the scope for the existence of the binding and, only if it exists, bind it to the actual DOM event. Example code:
scope:{
localClickFunc: "=?click",
localDbClickFunc: "=?dblclick"
},
link: function(scope, element, attrs) {
...
if( scope.localClickFunc != null ) {
element.on("click", scope.localClickFunc);
}
...
}
Then use the directive as:
<myButton click="localClickFunc($event)"
dblclick="myLocalDbClickFunc($event)"
/>
You can specify any or all of the attributes and the handlers will be installed accordingly.

u can send the attr like say the directive name is mydir then and then in the link and compile functions one of the 3rd parameter is usually names as params and u get there by params.mydir (value will be ovidiu). you can send anything textbased like ';' seperated like "click;dbclick;onmouseout" and break it in the directive with params.mydir.split(';'). from there you can even go to groups of events like "allClicks", u just need in the link some dictionary or switch that will append the right stuff.

Related

Remove a property by name in OpenMesh

In OpenMesh, once a named property is added to an element, it will be permanent in the sense that the property survives the scope of the property manager as explained here. My question is, how to remove such a property by its name?
So far I tried removing by the property manager and even this one fails:
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
mesh.remove_property(face_props);
with the error
error: no matching function for call to ‘OpenMesh::TriMesh_ArrayKernelT<>::remove_property(OpenMesh::PropertyManager<OpenMesh::FPropHandleT<FaceProp>, int>&)’
Is there a remove_property function where I could write remove_property("face_props") (or similar) to remove the property?
Edit: The following gives the same error:
mesh.remove_property( OpenMesh::getProperty<OpenMesh::FaceHandle, FaceProp>(mesh, "face_props") );
I suspect that mesh.remove_property() expects a property handle object, but getProperty() returns a property manager. I don't see how to get around this.
Edit2: I guess an alternative question would be: how to get a property handle to a property from a property manager?
Edit3: Looking at the source, it seems like PropertyManager has a member function deleteProperty() but a) is private and b) it only deletes the property if retain is not set which I assume would be set for named properties.
Apparently one can define a lower level property handle and then use get_property_handle which takes a handle as reference and updates it in place. This works:
OpenMesh::FPropHandleT< FaceProp > fprop;
mesh.get_property_handle(fprop, "face_props");
mesh.remove_property( fprop );
I wish this was better documented. Or that I was better in C++.

React Native arrow functions and 'if' statements

I have just been schooled on arrow functions, and how they can aid with visibility when you start using sub-functions on React Native and globally accessible objects.
I am not sure if this is different for "if" statements, but I can't get this to work at all. The issue:
myFunction() {
console.log('Welcome Flag: ' + this.props.welcomeFlag);
if (this.props.welcomeFlag == false) {
this.props.dispatch(setWelcomeFlag(true));
showMessage('Welcome back, ' + this.props.userName + '!', { duration: 3000 });
}
}
In this example, the console logs the initial value of welcomeFlag, which is "false". I would then like to, if it is false, display a message to the user and set it to true. Super simple stuff.
It falls over here:
this.props.dispatch(setWelcomeFlag(true));
Because my if statement is not an arrow statement.
Except I can't get the arrow statement to work for if statements. It is working for other kinds of statements, but just not for these.
I have tried the answers listed here:
How to use if-else condition in arrow function in JavaScript?
But none of these work.
How can I fix it?
myFunction() is a class level function. Binding anything to this is referring a class itself, therefore you need to bind it to access this property, otherwise you won't be able to access the class level props.
Although there are many ways to bind this as explained here, but the simplest one would be to use the shorthand arrow syntax as
myFunction = () =>

Using preprocess hook on specific node type in Drupal 8

I've had success using preprocess page hooks such as:
function mytheme_preprocess_page__node_front(&$variables) {
...
}
and
function mytheme_preprocess_page__node_12(&$variables) {
...
}
which correlate with custom templates named page--front.html.twig and page--12.html.twig, respectively.
I'm trying to implement the same hook and template pairing for a content type called Video. I understand that there is a difference in that my examples have been custom templates for specific pages while my goal is a custom template for an entire content type, but I got a custom template called node--video.html.twig that works as a template for all video pages. However when I try to write a hook based on this template name:
function mytheme_preprocess_node__video(&$variables) {
...
}
this does not work. I think that I either can't define a hook like this, or I'm just naming it incorrectly. I found a couple threads somewhat relating to this such as this that seem to imply that I need to define a hook for all nodes and then write an if statement that handles each type separately.
So.......
Final Question: Can I define a hook for an entire content type, and if so what am I doing wrong?
Use condition within the preprocessor to get the node type and then either do your logic within, or invoke another function.
function mytheme_preprocess_node(&$variables) {
switch ($variables['node']->getType()) {
case "video":
// ...
break;
case "something_else":
// ...
break;
}
}
You could in theory emulate what you are trying to achieve by trying to invoke a function named mytheme_preprocess_node__" . $variables['node']->getType() if it exists, but is too much fuss without a clear benefit.
In drupal 7 from zen template I used to use this generic solution. I think it is still a viable solution on drupal 8:
function mytheme_preprocess_node(&$variables) {
...
// Add global modification that works for all node type
$function = __FUNCTION__ . '__' . $variables['node']->getType();
// Each node type can have its own specific function
if (function_exists($function)) {
$function($variables, $hook);
}
...
}
You can then now add preprocess function that will only works for you node type.
function mytheme_preprocess_node__video(&$variables) {
...
}
Instead of having one big function, each node type's preprocess logic has its own function. It's better for maintainability.

cfscript component function with object types in arguments and returntypes

I have a feeling this is a bug in CF9, from looking at this: How to specify argument attributes in CFscript? (CF9)
However, if not, I'm writing a cfscript component in CF9 (pure), and attempting to pass an argument as a type of user defined cfc.
public function init(required _lbr._core._sharing._access.accessLinkDAO oAccessLinkDAO) returntype="_lbr._core._sharing._access.accessLinkBusiness" {
But CF keeps coming back with:
You cannot use a variable reference with "." operators in this context
is this something broken with CF9 pure?
I have confirmed this is a bug in CF9.0 (and fixed in one of CF9.0.1 or CF9.0.2; probably 9.0.1).
However the fix is easy. The problem is only with the dotted paths, and as #ScottStroz points out, you don't need them. This works fine:
component {
public accessLinkBusiness function init(required accessLinkDAO oAccessLinkDAO) {
return this;
}
}
I've moved the return type simply because that's just the normal place for it: it'll work as attribute too (but that syntax is just awful).
If the CFCs you are referencing as return types or argument types aren't in the same dir as the CFC using them, use an import statement, eg in this case:
import _lbr._core._sharing._access.*;

Cakephp 2.1 Router::connect /* except some actions

I would like to know whether it's possible to have something like this next line but with the exclusion of some actions. Because I would like to have manage, add, delete,... to go to the respective action and not to the display action. I know it's possible by specifying these rules explicitely upfront, but if you have quite some of these it will not look to good in the router file.
Router::connect('/paginas/manage', array('controller' => 'paginas', 'action' => 'manage'));
...
Router::connect('/paginas/*', array('controller' => 'paginas', 'action' => 'display'));
So the aim is to remove the first line...
Thanks.
Do something like this in your PaginasController which is basicly the PagesController I think.:
....
public function display(){
// Assuming default behavior of cakephp here
...
if (!empty($path[0])) {
$page = $path[0];
if(
method_exists($this, $page) &&
!in_array(
$page,
array(
// Methods that never should be executed in this controller are going in here
)
)
){
$this->{$page}();
}
}
}
This would execute a specific method in the controller. You could exit it there to make it stop working afterwards.
This might be a bit dangerous though because it can access parent methods of AppController, too.
The other way in which you also need exactly two of those Router::connect() rules one described here: http://api.cakephp.org/class/router#method-Routerconnect
Router::connect(
'paginas/:action/*',
array(),
array('paginas' => '(manage|add|delete)')
);
This one goes first followed by the other one. Never tested!
Though I don't see why there should be public methods for editing pages. Use these to seperate them from the rest: http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
I recommend, if you are trying to do this what I am thinking of, that you won't start to write a management for pages using the PagesController. "Pages" is a more or less reserved word in cake and you can get in big trouble with using those words (wrote a "File" plugin once including a "File" model. Waste of time as if since cake1.2 or 1.3 there is actually a "File" class to handle file operations). Create something new like "ContentPage" or whatever for it. You are on a saver side then. This paragraph is maybe useless, because your controller is not even named "Pages", but I hate to delete long ones, so it stays, just in case you need this information once.
Also i recommend not to change programming language, which means, you either use english or spanish or whatever but not both or worst more. You could name your route whatever you want to, but the class names should maybe stay in english because cake is also.
Greetings
func0der