Permalink
Browse files

supporting registration of global models

  • Loading branch information...
1 parent baed690 commit f5251aafe36be9398c3be823721d23dc91be5acd @caridy committed Nov 17, 2012
Showing with 51 additions and 21 deletions.
  1. +51 −21 lib/app/addons/ac/models.common.js
@@ -4,6 +4,7 @@
* See the accompanying LICENSE file for terms.
*/
+/*jslint nomen:true*/
/*global YUI*/
/**
@@ -13,6 +14,8 @@ YUI.add('mojito-models-addon', function (Y, NAME) {
'use strict';
+ var _clientCache = {};
+
// TODO:
// - update tests
// - update fixtures
@@ -24,52 +27,79 @@ YUI.add('mojito-models-addon', function (Y, NAME) {
* Addon that provides access to the models collection
* @class Models.common
*/
- function Addon(command) {
+ function Addon(command, adapter) {
+ this._models = {};
+ this._adapter = adapter;
+ this._instance = command.instance;
+ }
+
+ Addon.prototype = {
- var models = {};
+ namespace: 'models',
/**
* Gets model instance
* @method get
* @param {string} modelName The name of the model.
* @return {object} model instance, or null.
*/
- // this is an experiment where "get" method uses the closure
- // rather than be directly attached, to avoid storing
- // a instance.config or models reference in the addon instance.
- this.get = Y.bind(function (config, modelName) {
+ get: function (modelName) {
- var modelInstance;
+ var model = this._models[modelName] ||
+ (this._adapter.req && this._adapter.req.models &&
+ this._adapter.req.models[modelName]);
// instantanting the model once during the lifetime of
// the ac object, this acts like an internal cache.
- if (Y.mojito.models[modelName] && !models[modelName]) {
+ if (!model) {
// We have to heir() otherwise this.something in the model
// will pollute other instances of the model.
- modelInstance = Y.mojito.util.heir(Y.mojito.models[modelName]);
+ model = Y.mojito.util.heir(Y.mojito.models[modelName]);
- if (Y.Lang.isFunction(modelInstance.init)) {
+ if (Y.Lang.isFunction(model.init)) {
// NOTE that we use the same config here that we use to
// config the controller
- modelInstance.init(config);
+ model.init(this.instance.config);
}
- models[modelName] = modelInstance;
+ this._models[modelName] = model;
}
// returning from cache if exists
- return models[modelName];
-
- }, this,
- command.instance.config /* config (first arg) */
- );
-
- }
+ return model;
- Addon.prototype = {
+ },
- namespace: 'models'
+ /**
+ * Set a model instance as global. On the server side
+ * this means any mojit instance under a particular request
+ * will have access to the model. On the client, any
+ * mojit instance on the page will have access to
+ * the model as well.
+ * @method get
+ * @param {string} name The model name.
+ * @param {object} model The model instance
+ */
+ registerGlobal: function (name, model) {
+ if (this._adapter.req) {
+ // server side routine to store on the request
+ // to avoid leaks.
+ // NOTE: models on req object will be destroyed
+ // with the request lifecycle.
+ this._adapter.req.models = this._adapter.req.models || {};
+ this._adapter.req.models[name] = model;
+ Y.log('Storing a global model on the server: ' + name, 'info', NAME);
+ } else {
+ // client side routine to store on a global
+ // cache structure.
+ // NOTE: there is no way to destroy this model at
+ // the moment, it is now tied to the page
+ // life cycle.
+ _clientCache[name] = model;
+ Y.log('Storing a global model on the client: ' + name, 'info', NAME);
+ }
+ }
};

0 comments on commit f5251aa

Please sign in to comment.