From: dsc Date: Mon, 16 Apr 2012 20:06:10 +0000 (-0700) Subject: Abstracts out ModelCache. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=239e23df98012c3a0967ef76215a88966d3cd73a;p=limn.git Abstracts out ModelCache. --- diff --git a/lib/base/index.co b/lib/base/index.co index eb20dbb..b3b17f8 100644 --- a/lib/base/index.co +++ b/lib/base/index.co @@ -1,5 +1,6 @@ mixins = require 'kraken/base/base-mixin' models = require 'kraken/base/base-model' views = require 'kraken/base/base-view' +cache = require 'kraken/base/model-cache' cascading = require 'kraken/base/cascading-model' -exports import mixins import models import views import cascading +exports import mixins import models import views import cache import cascading diff --git a/lib/base/model-cache.co b/lib/base/model-cache.co new file mode 100644 index 0000000..5bd459d --- /dev/null +++ b/lib/base/model-cache.co @@ -0,0 +1,40 @@ + +function ModelCache (ModelClass, ModelListClass) + ModelClass import do + CACHE : new ModelListClass + ready : false + + register: (model) -> + # console.log "ModelCache(#{@CACHE}).register(#{model.id or model.get('id')})", model + if @CACHE.contains model + @CACHE.remove model, {+silent} + @CACHE.add model + model + + get: (id) -> + @CACHE.get id + + lookup: (id, cb, cxt=this) -> + # console.log "ModelCache(#{@CACHE}).lookup(#id, #{typeof cb})" + unless @ready + @on 'cache-ready', ~> + @off 'cache-ready', arguments.callee + @lookup id, cb, cxt + return + + if @CACHE.get id + cb.call cxt, null, that + else + Cls = this + @register new Cls {id} + .on 'ready', -> cb.call cxt, null, it + this + + # Bind the ModelCache methods to the class + for m of <[ register get lookup ]> + ModelClass[m] .= bind ModelClass + + ModelClass + + +module.exports = exports = ModelCache diff --git a/lib/util/backbone.co b/lib/util/backbone.co index 4ecfb14..58f2688 100644 --- a/lib/util/backbone.co +++ b/lib/util/backbone.co @@ -12,6 +12,14 @@ window?.Backbone = Backbone Backbone.setDomLibrary that if window? and (window.jQuery or window.Zepto or window.ender) +_bb_events = + once: (events, callback, context) -> + fn = ~> + @off events, arguments.callee, this + callback.apply (context or this), arguments + @on events, fn, this + fn + /** @@ -89,7 +97,9 @@ _methodized = exports._methodized = _.reduce do o {} + +_.extend Backbone.Events, _bb_events for Cls of Backbone.<[ Model Collection View ]> - Cls import _methodized - Cls:: import _methodized + Cls import _methodized import _bb_events import Backbone.Events + Cls:: import _methodized import _bb_events