Example of using Handlebars lookup helper - handlebars.js

Handlebars has a built-in helper called lookup. The documentation is not very clear about how it works. Could I see an example?

Sure, past me! Here's an example from your future.
Suppose you have an object or array obj and a variable field and you want to output the value of obj[field], you would use the lookup helper {{lookup obj field}}.
The code defining the helper is simply:
function(obj, field) {
return obj && obj[field];
}

The lookup property is useful if we don't know the name of the property we want, for instance because it's in a variable or the result of an expression.
If we have this object:
var book = {
title: 'Discovery of Heaven'
};
We could put this in the HTML like this:
<p>{{book.title}}</p>
Which is equivalent to:
<p>{{lookup book 'title'}}</p>
Maybe we don't know that we want the title. Say the property name is somewhere in a variable instead:
var property = 'title';
Now we could show the book title like this:
<p>{{lookup book property}}</p>

Related

Handlebars variable inside each loop (Accessing object key value)

One of my components needs to fetch different data from a property based on a parameter I pass it. The parameterHolder is an Object with keys and parameter is the key name.
{{component-name parameterHolderBinding=parameterHolder parameterValueBinding=parameter}}
I'm trying to loop through it like so
{{#each item in parameterHolder.[{{parameter}}]}}
{{/#each}}
But it's not working. When I try,
{{parameterHolder.keyName}}
I'm getting [Object] which is correct. Could someone please point out the right way do this?
I've managed to get it working by dynamically creating new properties.
In my controller -
parameterUpdatedObserver: function ()
{
var reference = this;
$.each(parameterHolder, function (key, value) {
reference.set('parameterHolderName'+key, value);
});
}.observes('parameterHolder')
And my component -
{{component-name parameterHolderBinding=parameterHolderNameKey parameterValueBinding=parameter}}
I can probably abstract it up one more level using another component. If anyone has a better solution, please do post it.

create a dictionary to map enums to events

I'm trying to create a dictionary to map an enum to a set of events with the same signature . I wrote :
public enum Events {Insert, Update, Delete};
// this part makes errors
Dictionary<Events,EventHandler<T>> EventsDic = new Dictionary<Events,EventHandler<T>>()
{
{ Events.Insert , this.ItemInserted}
};
what's wrong ?
The problem is that T has to be replaced with a type since you are declaring a variable. T must be the type of the value in the dictionary or, in your specific case, the type of the event arguments. Since you specifically say you are wanting to store events, T should probably be the good old EventArgs. Your code should look something like this:
Dictionary<Events,EventHandler<EventArgs>> EventsDic = new Dictionary<Events,EventHandler<EventArgs>>()
{
{ Events.Insert , this.ItemInserted}
};

How to set an empty Object's properties programatically?

I'm doing some Actionscript work right now and I'd like to know whether there's a way to initiate an empty object's value programatically like this:
var myObj:Object = new Object;
myObj.add("aKey","aValue");
To add a property called aKey whose value is aValue
I need to create a "Dumb" (data-only) object to use as a parameter to send via POST. So I don't know offhand how long and/or how many attributes it's gonna have.
Or something like that.
Thanks
ActionScript 3 allows you to create new Objects using an expressive Object Literal syntax similar to the one found in JavaScript:
const myObj : Object = {
aKey: "aValue",
};
trace(myObj.aKey); // "aValue"
If you want to assign properties after the object has been constructed then you can use either dot notation or square bracket notation, eg:
const myObj : Object = {}; // create an empty object.
myObj.aKey = "aValue";
myObj["anotherKey"] = "anotherValue";
If you plan on sending the data over HTTP, you may wish to consider looking at the URLVariables class which will take care of URL encoding the data for you.

I need help doing a Regex.Replace with multiple results

I'm building a custom page caching utility that uses a syntax like {Substitution:GetNonCachedData} to get data that's not supposed to be cached. The solution is very similar to the built-in <# OutputCache %> stuff but not as flexible (I don't need it to be) and, most importantly, allows the session state to be available when retrieving non-cached data.
Anyway, I have a method that replaces the tokens in the html with the result of the static method named in the {Substitution} tag.
For example my page:
<html>
<body>
<p>This is cached</p>
<p>This is not: {Substitution:GetCurrentTime}</p>
</body>
</html>
will fill in the {Substitution:GetCurrentTime} with results of a static method. Here's where the processing happens:
private static Regex SubstitutionRegex = new Regex(#"{substitution:(?<method>\w+)}", RegexOptions.IgnoreCase);
public static string WriteTemplates(string template)
{
foreach (Match match in SubstitutionRegex.Matches(template))
{
var group = match.Groups["method"];
var method = group.Value;
var substitution = (string) typeof (Substitution).GetMethod(method).Invoke(null, null);
template = SubstitutionRegex.Replace()
}
return template;
}
the variable template is the html with custom tokens in it that need to be replaced. The problem with this method is that everytime I update the template variable with the updated html the match.Index variable no longer points to the right character start because of the template now has more characters added to it.
I can come up with a solution that works by either counting characters etc or some other screwball way of doing it, but I first want to make sure there's not some easier way to achieve this with the Regex object. Anyone know how to this?
thanks!
You should call the overload of Regex.Replace that takes a MatchEvaluator delegate.
For example:
return SubstitutionRegex.Replace(template, delegate(Match match) {
var group = match.Groups["method"];
var method = group.Value;
return (string) typeof (Substitution).GetMethod(method).Invoke(null, null);
});
Instead of using Matches and looping on the results, set the regex to compiled and use a single Match in a while loop until it stops matching.

flex 3 and using name/value pairs

Does anyone have any recommendations for working with name/value pairs in Flex?
Context:
I want to create a Flex UI for updating a database table. I want the UI to contain the field name(read only) and current field value (which will be editable). Im using the WebService component to retrieve the field value and then asssigning it to an object with the field name hardcoded e.g.
private function resultHandler(event:ResultEvent):Object
{
var resultsObj:Object;
resultsObj = {
name:event.result.name as String,
This approach however is adding the dependency that the table structure/field names will never change. Using the object type also requries that i write my own algorithm to sort the output.
I'm not sure I understand the issue.
If you want to avoid dependency of the properties returned from the event at this point, simply use the event.result object, which is already an associative array.
As for sorting, we would need more context on what you are attempting to sort.
That's pretty simple. Don't use for each in loop. Use For-In Loop
Demo is shown below.
var dicEntry:Object = new Object();
dicEntry["Name"] = "Raj";
dicEntry["sal"] = 10000;
dicEntry["age"] = 33;
for(var key:Object in dicEntry)
{
trace("Object Key: "+key+" Object Value: +dicEntry[key]);
}
That's it.

Resources