How to get originating context of helper in handlebars? - handlebars.js

I am creating a custom helper.
If I use my new helper within an object like this:
{{#data}}
{{newHelper}}
{{/data}}
How do I access the data object from my helper function?
I know I can do
args.data.root['data']
But I want to access it dynamically because it wont always be within an object called 'data', it could be anything.

You can access the current context with this
So instead of using args.data.root['data'].value you can simply use this.value

Related

Handlebars Accessing Non-Related Object in Nested Each Statement

I have a scenario where I am using a registered helper ifeq conditional that is comparing the object being used within my each statement and a separate object that is public to the view file. i.e. blog_comment and user object.
However, I can't seem to figure out a way to access the user object for the fact that it doesn't have a relationship to the each statement of blog_comment. Is there a way to access non-related objects within a handlebars expression?
Two objects that are publicly accessible by the view:
blog_comments (Being looped through)
user (represents the information of the user currently logged in)
Here is my ifeq conditional:
hbs.registerHelper('ifeq', function(value1, value2, options){
return((value1 === value2) ? options.fn(this) : options.inverse(this));
});
Here is my view file: (comparing blog_comments.userId to user.userId)
{{#blog_comments}}
<i>{{createdAtDateSlug}}</i>
{{#ifeq userId user.userId}}Delete</p>
{{/ifeq}}
<p class="blog-comment">{{comment}}</p>
{{/blog_comments}}
I figured out the solution. Since I am trying to access a separate object, I need to get back to the root of the scope and then access the object and its property. i.e. #root.user.userId in {{ifeq userId #root.user.userId}}. This change resolved my issue

Access DevExpress JavaScript objects for nested fields

When I render DevExpress MVC controls for nested fields, using a custom FormBuilder class of my own, I'm using the naming convention of ModelName.FieldName. This fixes binding issues I was having, but I now need client-side access to the JavaScript objects that are created for the control.
In the past, I would find an object called FieldName. For nested fields, what is the naming pattern for the JavaScript object name (since ModelName.FieldName would not be a suitable JavaScript object name), and is there perhaps an alternative way to access the object by passing in the full field name as a parameter to some JavaScript method (something like AllControls.GetControl('ModelName.FieldName'))?
Here is a sample of my code:
var textBoxSettings = new TextBoxSettings
{
Name = "ModelName.FieldName",
};
writer.Write(devExpress.TextBox(textBoxSettings).GetHtml());
UPDATE:
It appears that DevExpress does indeed render objects with the name ModelName.FieldName. I'm not sure how to access those objects in JavaScript - is there some kind of escape sequence to treat the entire phrase as a single variable name?
From my understanding the 'DevExpress' way to access controls dynamically is to use the control collection functions
var nameTextBox =
ASPxClientControl.GetControlCollection().GetByName('ModelName.FieldName')
DevExpress does actually create the JavaScript object with a variable name in the form ModelName.FieldName.
To access that object in JavaScript, you can use the format:
var myControl = window['ModelName.FieldName'];

Workflow: Trying to pass an object variable to a class method

I am having a tough time trying to pass a parameter into my method when I go to invoke it.
I new up a variable called setupNewCase, then I populate it with data. Once I do that, I then try to invoke the method. It normally works when I don't have a value to pass into it, but it doesn't work here in this case as I am trying to pass something into it.
To pass parameters to the invoked method you use the Parameters property on the InvokeMethod activity. Its available on the activity's properties grid and not directly on the designer.
You would use TargetObject when invoking a method on an instance object but since your CommonMethods class is static you won't ever instantiate it and therefore TargetObject should be empty.

How can I pass #Model to Angular ng-init

Isn't it possible to pass #Model to ng-init in ASP.NET MVC 4 view? When I do it like data-ng-init=init(#Model) it is undefined in the init() function.
$scope.init = fuynction(model){
console.log(model); // prints undefined
}
By the way I'm new to AngularJs. Any help is appreciated in advance.
As I commented, you need to translate between your server-side objects (e.g. #Model) and client-side objects.
For example, if you just wanted to use the Name property of #Model you could do something like this:
<div ng-init="init('#Model.Name')"></div>
$scope.init = function(name){
console.log(name); // prints value of name
}
Notice, that even in this simple example, it is necessary to format #Model.Name into a JavaScript string (by putting quotes around it).
Probably, you don't want to use the entire #Model object. It is best to choose only the fields you want and pass them in individually.
If you need access to a lot of data from the server, it is recommended to use $http service and make a server call for the data.
Serialize you model into json string, using Newtonsoft.Json assembly or similar and pass the serialization result to data-ng-init.
inlcude the #using ProjectApp.Models then data-ng- init="init(#Newtonsoft.Json.JsonConvert.SerializeObject(Model))"

Is there a way to get twig to consider a method of a passed object as safe?

I am having issues with twig escaping output on method calls to an object passed into my twig template. I know I can get around this with the |raw filter, but was hoping there might be a way I could simply specify in my object that certain methods are safe and therefore remove the need for the raw filter.
Method calls of objects itself cannot be made html safe because normal objects/entities are not (and should not be) aware of the template engine.
However, a twig filter or function if aware of the template engine and can be marked html safe in its definition.
So what you need to do is to implement a html safe twig filter to pass the object to and call the method of your object inside the filter function.
I guess your templates looks like this:
<p>{{myObj.getHtmlRepresentation()|raw}}</p>
Now you need to implement a twig filter and change the template to the following:
<p>{{myObj|html_representation}}</p>
And the twig extension should look like this:
class MyTwigExtension {
public function getFilters(){
return array(
// the magic is the is_safe=>html option
'html_representation' => new \Twig_Filter_Method($this,'htmlRepresentation',array('is_safe'=>array('html'))),
}
public function htmlRepresentation($obj){
return $obj->getHtmlRepresentation();
}
}
One design consideration: If your object is an entity of a business object of some kind, it sould not create html but you should move the html creation to a template or the twig filter..

Resources