Why is variable substitution only recommended for values that wont change during lifetime of a widget? - dojo

Regarding Variable Substitution in Dojo
Variable Substitution:
A template can have values set on DOM rendering
though the use of a simple variable placeholder syntax, which looks
like this:
${property}
According to the documentation
Variable substitution in a template is only recommended for values
that will not be changed during the lifetime of the widget. In other
words, if you expect to be able to set the value of a property in a
widget during the lifetime of your application programmatically, we
recommend instead using your widget's postCreate method to set any
variables programmatically through your widget's set() method.
Can someone explain why this recommendation is made?

Variable substitution in Dojo has no binding.
This mean it wont change even if you change the actual value of the variable.
If a binding is needed, then you can use an attach point and a setter for that value. It will then have a binding and the UI will be updated with the new value.
Something like this:
_setLabelAttr : {
node : "_tplLabelNode",
type : "innerHTML"
},
Will bind the innerHTML of attach point _tplLabelNode to the "label" property of the widget.
So widget.set('label', 'foo'); will update the UI.
However <div>${label}</div> has no bind. ${label} will be replaced at creation of the widget and will never be updated

Related

How to assign a variable in MVEL Template

I would like to assign variables in an mvel template.
Assuming that my template has only a property foo defined, I would like to do the following:
#{bar=foo}
#{bar.name}
Unfortunately this outputs me the toString() of foo and then the property name of bar. I would like to do the same but without printing anything with the assignation.
(just in case anyone lands here...)
According to this documentation, you can use #code blocks to execute statements without emitting output.
#code{bar=foo}
#{bar.name}

Resharper -is there a way to have it automatically update references?

I saw someone using resharper and I noticed that whenever they changed a property name it was instantly reflected in all references to it.
So I downloaded a trial of resharper and it seems it is still a manual 'replace all' feature. It is not updating live as I type.
Resharper looks cool, but I want this particular feature that I saw! And at a dead-end with Google.
The thing you saw was probably a Live Template. Those can contain placeholders, and the same placeholder can appear more than once within the same template. When you insert the template, the cursor goes to the first placeholder, where you can type in a new value, and all the other instances of that same placeholder will reflect those same edits as you type.
For example, ReSharper comes with a "dependencyProperty" Live Template, which is defined as:
public static readonly System.Windows.DependencyProperty $propertyName$Property =
System.Windows.DependencyProperty.Register("$propertyName$", typeof ($propertyType$), typeof ($containingType$), new PropertyMetadata(default($propertyType$)));
public $propertyType$ $propertyName$
{
get { return ($propertyType$) GetValue($propertyName$Property); }
set { SetValue($propertyName$Property, value); }
}
When you insert this template, your cursor goes to the first placeholder (indicated as $propertyName$ above), and you can type a value for that placeholder. All instances of the $propertyName$ placeholder will update with those same edits as you type.
Then you can hit Tab, which moves you to the next placeholder ($propertyType$), which you can similarly update. If you Tab again (to leave the last placeholder), then you exit placeholder mode and the template becomes plain old text that you can edit normally.
You also get live-update-as-you-type behavior when you rename a local variable. But if you saw this with a property, it was almost certainly a Live Template. (Property renames could affect multiple files, which could take time, so ReSharper waits until you confirm the rename before they do the work. It will also suggest related renames, like doing a corresponding rename to a property's backing field, which also isn't practical with live updates.)

How to use variable value in live templates in Intellij IDEA?

I want to create live template for setter.
I've created this template
How can I use value of par variable to generate value of var variable? Basically, I want to avoid redundancy here and put name of variable only once and other one will be generated automatically by some algorithm.
UPDATE
I want to clarify a little bit what I want to achieve.
Suppose I want to create setter with name setTime which has parameter time.
public void setTime(long time)
{
// ...
}
I don't want to type "time" twice - capitalized and non-capitalized. I want to type just parameter name so method name will be generated automatically.
UPDATE (Answer)
Turned out that variable order is important. This is final result of what I want
You can use the soutv as an example, notice how it defines a copy of a variable:
It's also possible to define custom expression for live templates via plugins or Groovy code:
How can i add custom expression functions for Live templates in Intellij.

How can I print a servlet context attribute in EL?

Is there a way I can get a attribute set in ServletContext in EL so that it ends up as a JavaScript variable?
I am setting it as
context.setAttribute("testing.port", "9000");
I tried retrieving it like
alert("port" +'${testing.port}');
I am just getting a blank.
The problem is the period (.) in the key name. EL interprets the period as a call to an accessor method named getPort1 on whatever object testing references. Fetch the value from the appropriate implicit object:
${applicationScope['testing.port']}
or just use a different key:
${testingPort}
1Yes, this is a simplification of what really happens. It may also look for a predicate getter named isPort, or try Map#get("port").

How to use the data store to set a toolbar title in Sencha Touch

I am trying to set the toolbar item dynamically. So far I have a back button that resets the toolbar title to 'start' if the user chooses to go back.
But the following code won't work:
menuList.on('itemtap', function(dataView, index, item, e){
viewport.dockedItems.items[0].setTitle('{title}');
});
It tries to use a variable called 'title' out of my data store array. This works great for providing text to my Ext.List items. But the above code sets the toolbar title to the string '{title}' without even thinking of it being a variable.
Can you help me out?
List's use templates so items within curley braces get evaluated... you'll need to pass a reference to a variable without quotes. You haven't provided enough code for me to tell you where that information would be. If you already have a variable in scope called title that you put the data into then you can just reamove the '{ and }' ... otherwise you'll need to get the data you need from your store through some means, like Ext.StoreMgr or [appname].stores
Two things. 1) You will really want to get used to digging into the ST source code. In this case, if you look at the code for "setTitle", you will see that its argument is interpreted as straight HTML, not a template. So you can't use curly bracket syntax here. 2) Note that the type of the "item" argument to the event handler is an Element (i.e. ST's representation of the DOM object, not the selected datastore object. So that's not going to help you. However, the "index" arg gives you an easy way to get the appropriate object from the store. i.e.
[appname].stores.pages.getAt(index).title
I really don't know why, but it works if you put up to variables: One for the record and one for the value inside that record. There is a detailed explanation in the sencha.com-forum