I got things working. It probably isn't the optimal way of doing this (findAll gets called twice), but...
My mistake in my original post was attempting to use the Instances object from the findAll success callback as the data that I wrapped in a Deferred object. I had to use the return value of the findAll method and wrap that in the Deferred object. Below is my now working code.
Bonus question: Is there a way for me to not call findAll twice? With the return value from the findAll method, is there a way to inspect the "data.total_count" object in my code below without using the success callback?
- var Foo = can.Model ({
- findAll: function(params) {
- var returnVal;
- var localStore = {};
- localStore = FooLocal.findAll();
- FooLocal.findAll({}, function(data) {
- if (!data || data.total_count == 0) {
- returnVal = can.ajax({
- url: "/endpoint",
- type: "get",
- success: function(data) {
- new FooLocal(data).save();
- }
- });
- } else {
- returnVal = new can.Deferred;
- returnVal.resolve(localStore);
- }
- });
- return returnVal;
- }
- }, {})
- var FooLocal = can.Model.LocalStorage ({
- storageName: 'foo-canjs'
- }, {});
- can.view( "foo.ejs", Models.Foo.findAll() )
- .then( function (frag) {
- $("#app").html(frag);
- }
- );