I'm using m.request in a project, and since I have a request that can be potentially long running, I want to run it with background:true
. However, it seems like the value never gets set to the generated m.prop
.
I've made a jsFiddle with an example based on this Stack Overflow answer: http://jsfiddle.net/u5wuyokz/9/
What I expect to happen is that the second call to the view should have the response value in ctrl.test.data()
, but it seems to still have undefined
. At Point A
in the code, it logs the correct value. However, at Point B
, it logs false, undefined
and then true, undefined
.
I'm not sure if I'm doing something incorrectly, or if this the expected behavior.
Code from the jsFiddle:
var requestWithFeedback = function(args) {
var completed = m.prop(false)
var complete = function(value) {
completed(true)
return value
}
args.background = true
return {
data: m.request(args).then(complete, complete).then(function(value) {
//Point A
console.log(value);
m.redraw()
return value
}),
ready: completed
}
};
var mod = {
controller : function() {
this.test = requestWithFeedback({
method : "POST",
url : "/echo/json/",
serialize: serialize,
config: asFormUrlEncoded,
data : {
json : "{\"name\" : \"testing\"}"
}
});
},
view : function(ctrl) {
//Point B
console.log(ctrl.test.ready(), ctrl.test.data());
return m("div", ctrl.test.ready() ? 'loaded' : 'loading');
}
};