Magento: How to pass a variable from block to a Model - magento

I am using an extension where I need to pass the variable is moving from block class back to model, I have used session it works sometime but not uniformly,
Is there any other way to pass it,
Thanks in advance,

try using Magento’s Registry Pattern
The three registry methods are
Mage::register
Mage::unregister
Mage::registry
The register method is how you set a global-like variable.
Mage::register('some_name', $var);
Then, later in the request execution, (from any method), you can fetch your variable back out
$my_var = Mage::registry('some_name');
Finally, if you want to make you variable unavailable, you can use the unregister method to remove it from the registry.
Mage::unregister('some_name');
Source

If you can use the Model as a singleton, you can try this:
Mage::getSingleton('yourmodule/yourmodel')->setStuff('xxxx');
and later
Mage::getSingleton('yourmodule/yourmodel')->getStuff();
if you don't know what singletons are you should maybe try the registry approach from epynic to prevent problems.

Related

How to pass context between asynchronous method calls in Python 3.5?

How can I pass context from on asynchronous method call to another one - without using method parameters?
I need the feature to enrich log messages with a kind of flow ID so that I can easily trace all log messages of a specific call method flow.
I use Python's async and await keywords (Python 3.5.x).
You should use Context Variables introduced in Python 3.7, and I have a polyfill for Python < 3.7 as aiocontextvars.
Previous answer:
You may want to take a look at tasklocals and aiolocals.
I solved the problem by setting a custom task factory. It turned out that having a context per task (in comparison to a context per async call) was sufficient for me.
I'm working on aiotask-context package. It's a really simple way of passing context between tasks (called with await or yield from). If you don't wan't to use the package you can still use the idea :).
I'm working on how to propagate it for the ensure_future calls too.
import contextvars
c_id = contextvars.ContextVar("context_id", default=None)
def get_context_id():
return c_id.get()
def set_context_id(value):
c_id.set(value)
I struggled a lot for getting this right. If anyone is still searching for the answer then they can refer here. This works with Python3.7 onwards.
Create an instance of contextvars.ContextVar.
Here you can give the context variable name and a default value for that variable, the default value will be used in case the variable is not found in the current context.
Set the value using the setter and you can get the same value using the getter inside same context.
Define ContextVar at the top level once, and not inside closures(i.e. functions or class etc.) as garbage collection for context is not proper.

best practice to get the default object wrapper?

when creating custom method, I implements TemplateMethodModelEx and returns SimpleSequence object.
according to the API, I should use this constructor:
SimpleSequence(ObjectWrapper wrapper)
since I am setting incompatibleImprovements as 2.3.24, the doc said I can simply use Configuration instance's getObjectWrapper(). My problem is when implementing TemplateMethodModelEx, I have no access to the current config unless I pass cfg to the method's constuctor. then the root.put would look like:
root.put("getMeList", new GetMeListMethod(cfg));
this looks odd to me, i wonder whats the right to construct this kind of SimpleSquence model and whats the right way to get the default object wrapper.
Thanks a lot
You should pass in the ObjectWrapper as the constructor parameter. (It's unrelated to incompatibleImprovements 2.3.24.) Any TemplateModel that creates other TemplateModel-s (like TemplteSequenceModel-s, TemplateHashModel-s, TemplateMethodModel-s) used to work like that. This is normally not apparent because they are created by an ObjectWrapper. If you do the TemplateModel-s manually however (which is fine), then you will face this fact.

Get Class of Map in FreeMarker

I want to get a variable's class type in freemarker, used var.class.simpleName;
but if var is a Map, freemarker will process class as a key to find value in var.
it throw exception. how can I do this ? thanks for any suggestion.
First I have to ask why do you need that, because FreeMarker templates aren't supposed to know even if var is Map at all. Maybe your data-model is not what the template needs.
Anyway, for now, I would write a custom TemplateMethodModelEx for this purpose, something that you can use like ${classOf(var)}. Inside the TemplateMethodModelEx implementation you will receive a TemplateModel as the argument value, and then you can check if it's an AdapterTemplateModel, and if so you can get back the original object and get its class. (If it's not a AdapterTemplateModel, then it perhaps isn't even a wrapped Java object, so it doesn't make sense to ask what the class of the original object is.) However, the DefaultObjectWrapper with incompatibleImprovements set to less than 2.3.22 doesn't give AdapterTemplateModel to wrapped Map-s... so in 2.3.21 you will still have to use BeansWrapper, but you can at least set simpleMapWrapper to true.
In 2.3.22 it will be actually possible to write ${var?api.class}... you might use the nightly build. Though it only supposed to solve the problem where you can't access business methods because the primary type of the business class is Map.

Custom xmlsitemapprovider can't set Title of sitemapnode?

I created my custom XmlSiteMapProvider. Works out great but I want to be able to change the Title of the SiteMap also in there. However I get exceptions that Title is suddenly readonly while the property has a setter when viewing in the Object Browser. Anyone can explain this behavior?
I've been trying to create a new node, use the RemoveNode method to delete the current one and then use the AddNode method. However when I use the method with 1 parameter it starts to complain (exception that the parentNode can't be null). If I use the method overload with 2 parameters and want to pass in the parentNode of the node to delete it's also null. How to solve this?
Apparently it's not possible due to optimizations by Microsoft. The base class to be used instead is StaticSiteMapProvider.

How to find the current name of the test being executing?

I know I can figure out the name of the method as its being executed, just wondering if there is a way from the setup method. I guess an attribute method would work but from the setup method it would be the best.
EDIT NUnit
I know this is going to sound negative, but don't do it! :-)
The idea behind the setup method is that it executes something required by every test, which means that it doesn't matter which test is being executed, so you don't need to know the name of the method.
If you are after different data used in initialisation, then call a separate method with the data passed as a parameter from your test method.
If you really want what you are asking for, then you may need a different method that takes the name of the current method as a parameter and call that from your test method.

Resources