I'm running into some issues with moving from JMVC to CanJS, and I'm wondering is the what the "right" way to do the following. With JMVC, I used to do something like this in my model:
Key points:
1. "models" points JMVC to where the "model" data is in the response body
2. findAll takes success and error callbacks
3. $.ajax success handler is a chain of functions that each take the return value of the previous
#1 seems to work with findAll but not findOne. I haven't fully investigated this yet, but I mention this because the framework's converting data to models is a key part of the problem.
For #2, CanJS doesn't pass the success or error callbacks along. It DOES seem to call them internally, but somewhere along the line, findAll/findOne are invoked with ONLY the params argument. I understand that the deferred methods (done/fail/always) makes these callbacks somewhat less important, but this seems to break a very common pattern, and I question the wisdom of this behavior.
For #3, notice that the functions I chained cause my raw data to be converted to models, the models to be passed to a caching function, and then ultimately passed along to the caller's success handler.
How can I achieve this using only done/fail/always? If I say:
"data" in "done" is the raw response, not the converted list of model instances. It seems like the earliest I get the converted instances is in the caller's success callback (the one that doesn't seem to get passed to findAll anymore). But that sucks because I want caching to be handled internally by the model, not my controller.
I'm hoping I'm missing some very basic. If someone could point out the error of my ways or point me in the right direction, I'd really appreciate it. I was really looking forward to moving up to CanJS, but this is an example of the kind of friction that are making the experience not so painless and fun.
Thanks!
models: function(data) {
return data.nested.results;
},
findAll: function(params, success, error) {
var xhr = $.ajax(
{ // misc params },
this.proxy(["models", this.addToCache, success]),
error
);
return xhr;
}
Key points:
1. "models" points JMVC to where the "model" data is in the response body
2. findAll takes success and error callbacks
3. $.ajax success handler is a chain of functions that each take the return value of the previous
#1 seems to work with findAll but not findOne. I haven't fully investigated this yet, but I mention this because the framework's converting data to models is a key part of the problem.
For #2, CanJS doesn't pass the success or error callbacks along. It DOES seem to call them internally, but somewhere along the line, findAll/findOne are invoked with ONLY the params argument. I understand that the deferred methods (done/fail/always) makes these callbacks somewhat less important, but this seems to break a very common pattern, and I question the wisdom of this behavior.
For #3, notice that the functions I chained cause my raw data to be converted to models, the models to be passed to a caching function, and then ultimately passed along to the caller's success handler.
How can I achieve this using only done/fail/always? If I say:
findAll: function(params) {
var xhr = $.ajax({ // misc params })
.done(function(data) {
console.log(data)
});
return xhr;
}
"data" in "done" is the raw response, not the converted list of model instances. It seems like the earliest I get the converted instances is in the caller's success callback (the one that doesn't seem to get passed to findAll anymore). But that sucks because I want caching to be handled internally by the model, not my controller.
I'm hoping I'm missing some very basic. If someone could point out the error of my ways or point me in the right direction, I'd really appreciate it. I was really looking forward to moving up to CanJS, but this is an example of the kind of friction that are making the experience not so painless and fun.
Thanks!