These are chat archives for madglory/backbone-fetch-cache

22nd
Dec 2014
Beard of War
@blainsmith
Dec 22 2014 20:24
regarding mixin issues: getCacheKey checks if the instance has its own version of getCacheKey then it calls that instead, but since the mixin is adding a ‘getCacheKey’ function the it ALWAYS has it on it so it runs into a loop
MixinModel = Backbone.Model.extend(
  _.extend({}, Backbone.fetchCache.mixins, {
    url: '/my-mixin-model'
  })
);
Backbone.fetchCache.mixins = {
    setCache: Backbone.fetchCache.setCache,
    clearItem: Backbone.fetchCache.clearItem,
    getCacheKey: function(opts) {
      return Backbone.fetchCache.getCacheKey(this, opts);
    },
    getLastSync: function(opts) {
      return Backbone.fetchCache.getLastSync(this, opts);
    }
  };
so with this the MixinModel will have a function getCacheKey set right on it
function getCacheKey(instance, opts) {
    var url;

    // If the model has its own, custom, cache key function, use it.
    if (_.isFunction(instance.getCacheKey)) {
      return instance.getCacheKey(opts);
    }
when it calls getCacheKey above it first checks to see if the instance has its own function, which it does from the mixin so it just calls itself…again…and again
if we change the mixin function name to something other than getCacheKey it works fine since the name is different
Beard of War
@blainsmith
Dec 22 2014 20:31
maybe we standardize on a namespace convention for these mixin function like setFCItem, clearFCItem, getFCKey, etc.
and leave the functionality in place to define your own getCacheKey method as it already is…this keeps the names different and the mixins working properly
Kris Walker
@kixxauth
Dec 22 2014 20:43
@blainsmith the other thing I was thinking of is namespacing them the way you originally had them. So, the mixin would only add a fetchCache property to each instance, with the above methods attached to it.
Also, the instance.getCacheKey method is not out in the wild yet, so you could still change the way that gets attached.
Beard of War
@blainsmith
Dec 22 2014 21:01
cool, yeah i’ll have to play with this some more, running into a context issue after nesting them in fetchCache
Kris Walker
@kixxauth
Dec 22 2014 21:23
Yeah, we might be pushing a rock up a hill here :cry: I mean, when you think about it, the model instances should be associated with a key, but not really the cache functionality, since there could be many instances associated with the same key.