Try our conversational search powered by Generative AI!

Håkon Nordli
Jan 8, 2016
  5266
(0 votes)

Dojo - How to keep the widget's scope

When binding an event in JavaScript the scope outside the binding is not the same as inside. Which means that "this" is not the same outside as inside the binding.

Below there is a JavaScript example, which console.logs two different "this". The first console.log prints the object Foo, the second console.log prints the button object.

 

image

 

However, sometimes we want to keep the scope. This is often solved by putting "this" in a variable, like the example below. This way both console.logs prints the Foo object.

 

image

 

In Dojo there is a built-in function, hitch, which can keep the scope. This can come in handy when developing Dijit widgets.

In the example below hitch forces the on method to retain the original scope. In this way we keep "this" inside the callback, and we do not need to create a variable for "this" like we did above.

 

image

Jan 08, 2016

Comments

Please login to comment.
Latest blogs
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog