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
--- /dev/null
+
+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
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
+
/**
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