-
-
Save amcdnl/f14cbfc3738668457ebb to your computer and use it in GitHub Desktop.
define(['angular'], function(angular){ | |
var module = angular.module('utils.cache', ['restmod') | |
// Cache implementation for angular-restmod | |
// https://github.com/platanus/angular-restmod/issues/27 | |
module.factory('CacheModel', function(restmod){ | |
var mixin = restmod.mixin(function() { | |
var cache = {}; | |
// Cache overrides | |
// ------------------------------------------------------------ | |
this.classDefine('$fetch', function() { | |
var url = this.$url(), | |
cached = cache[this.$url()]; | |
if(cached){ | |
return cached; | |
} else { | |
return cache[url] = this.$super.apply(this, arguments); | |
} | |
}); | |
this.classDefine('$find', function(_pk) { | |
var url = this.$urlFor(_pk), | |
cached = cache[url]; | |
if(cached){ | |
return cached; | |
} else { | |
return cache[url] = this.$super.apply(this, arguments); | |
} | |
}); | |
// set a pointer for cache | |
this.classDefine('$cache', cache); | |
}); | |
return mixin; | |
}); | |
return module; | |
}); |
@iobaixas - no worries... get feeling better!
Back on track!
The first thing i noticed is that you should use $encode/$decode when writing/reading from the cache, like this:
this.on('after-fetch', function(){
cache.put(this.$url(), this.$encode());
});
this.on('after-fetch-many', function(){
cache.put(this.$url(), this.$encode()); // collection.$encode not available yet
});
Then in $fetch
:
this.classDefine('$fetch', function() {
var cached = cache.get(this.$url());
if(cached) {
// not sure if promises are needed here...
// $then will put the function in the object's promise chain and return the object itself.
return this.$then(function() {
this.$decode(cached);
});
} else {
return this.$super.apply(this, arguments);
}
});
after-save
should also use the $encode
to serialize the object.
Didn't know about $cacheFactory
, very nice, why do you generate a random ID for it each time?
re: cachefactory: so to make sure its unique. The name of the model its 'mixing' would be better but couldn't figure out how to get access to it. Got any thoughts?
re: $encode for collection: Let me know when thats available!
Do you have any thoughts on the 'singles' fetch. Ideally, I'd like to retrieve from the list but that gets tricky if the list is not there. Right now if i do a single fetch, I don't do any caching w/ it.
Also having issues with it underscoring everything. I thought it was camel by default?
I updated the implementation to reflect our new comments.
About the $singles
fetch, shouldn't it behave the same as any other fetch?
About the renaming, by default restmod does a underscore -> camelcase name conversion on decode and a camelcase -> underscore conversion on encode. Renaming can be disabled using the disableRenaming() builder method. What kind of issues are you having?
I missed the after-save
hook when I reviewed the code earlier today, some feedback:
if(!model){
collection.push(this.$encode()); // should use encode
} else {
// i think it would be better to just replace the cached object since $encode returns the complete object state.
angular.extend(model, this.$encode());
}
@iobaixas - Singles should behave the same yes and no. For instance:
var model = $restmod.model("/api/zoo")
model.$search() //-> [ { id: 1, type: 'panda' }, .... ]
but if I do:
model.$find(1) //-> { id: 1, type: 'panda' }
I would think that it would pull the panda object out of the previous search collection so that you dont have multiple references.
Now the $single
that you mentioned above, should response just the same I agree.
@iobaixas - Is there a way to disableRenaming
at a global / provider level?
Ups, didn't see that last question, about that, I just realised that the README is missing provider usage directions, you can set a base model in the config stage like this:
module.config(function($restmodProvider) {
$restmodProvider.pushModelBase(function() { // Dont really like the name for this method, any thoughs?
this.disableRenaming():
});
});
About the new implementation, I will miss the local storage support... Why aren't you caching single $fetch results?
To return results from $search
when doing a $find
with an id $search
returned you could do something like
this.on('after-fetch-many', function(xhr){
cache[this.$url()] = this;
angular.forEach(this, function(record) {
cache[record.$url()] = record;
});
});
@kenborge - Ya I thought about something like that. Managing the list(s) and singles together gets tricky.
Hi Austin, ive been pretty sick all day so i havent had the energy to focus on restmod today (of all days...). I'll take a look at this when I feel better, hopefuly tomorrow.