Quantcast
Channel: Dojo and Worklight Tips 'n' Tricks » async
Viewing all articles
Browse latest Browse all 2

Using Dojo Partial to mixin variables

$
0
0

Came across the same problem a few times now where I’ve wanted to get at my variables inside a dojo.hitch statement. This problem seems to crop up when performing an async request and then doing ‘something’ with it’s response. In the case below, our useful values are ‘undefined’ inside the hitch:

var attr = "badger";
var value = "apple";

//this is the result of an async datastore query
var gotItem = dojo.hitch(this, function(item) {
    //update item
   this.updateItem(item, attr, value); // <--- This will FAIL
});

The solution I have used before, is to mixin my values into the hitch scope, ie.

var scope = {"attr":attr, "val":val, "parent":this}
dojo.hitch(scope, function(){
    var val = this.val // <-- this works, but it's a bit nasty!
    //etc... etc...
})

However, using dojo partial mixes in the extra values as the first params to the hitch, with the item (from out previous example) being the final attr.

var attr = "badger";
var value = "apple";

//this is the result of an async datastore query
var gotItem = dojo.partial(dojo.hitch(this, function(attr, value, item) {
    //update item
   this.updateItem(item, attr, value); // <-- This will WORK!!
}), attr, value ); // <-- these values will be mixed in

So go on, try it!!



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images