Override a method in dojo - dojo.store.Memory

2019-08-26 06:59发布

问题:

Is there a way how to run my own function before a dojo method is spawned?

Specifically I need to refresh data in dojo.store.Memory before query() function is spawned. My idea is to put there a callback (that will be spawned before query()), fetch new data from server and then set the data to Memory instance. Then just call

this.inherited(arguments)

I've tried override query method with declare, but I'm still getting some unrelated errors. 4 hours but no luck...

Is there a another way?

Thanks

回答1:

Yes, you can fire callbacks before, after or around any method. Just use dojo/aspect

Something like this should work :

require(["dojo/store/Memory", "dojo/aspect"], function(Memory, aspect){
    aspect.before(Memory, "query", function(){
        // do something
    });
});

However, for your specific use case, if I understood correctly, what you want is to have a store linked to a server-side controller. In that case, you should use dojo/store/JsonRest rather than dojo/store/Memory. No need to fire any methods before the query...



标签: dojo