Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add store

  • Loading branch information...
commit 94b3f044a7c47be18eb13547cf70e046936473aa 1 parent cb33e8f
@rhysbrettbowen authored
Showing with 57 additions and 1 deletion.
  1. +7 −1 README.md
  2. +50 −0 store.js
View
8 README.md
@@ -68,7 +68,8 @@ model.set({
});
model.meta('name', ['firstName', 'lastName'], function(firstName, lastName) {
return lastName + ", " + Rhys;
-}); // returns "Brett-Bowen, Rhys"
+});
+model.get('name'); // returns "Brett-Bowen, Rhys"
```
## mvc.model.Schema ##
@@ -79,6 +80,10 @@ A schema can be set for a model. The schema takes in an object or map of keys an
A mvc.Collection extends mvc.Model and so has all of it's properties. Also a collection can contain an array of models that belong to it. A collection can keep these models in an order if given a comparator function and will also listen to changes and emit a change if any model it contains fires a change event.
+## mvc.Store ##
+
+This can be used as a factory and cache for models. Use the get to retrieve models by their ID and if they don't exist they will be created. This is good to make sure your models are unique. If no ID is passed then a new model will be created and you can get the model using it's CID until an ID is set for the model.
+
## mvc.Control ##
the closure library already provides goog.ui.Component which is a great controller. If you use Backbone.js you'll probably recognise it as the view. mvc.Control adds in two methods, delegateEvents and getEls. These are convenience functions. The getEls allows you to use simple string selectors to get a handle for the elements under the component and delegateEvents gives an easier interface for listening to events in the component. If you want to use a different class in the library that already extends goog.ui.Component you can stil use these functions by adding them to your classes prototype like so:
@@ -105,6 +110,7 @@ mvc.Router uses goog.History and hash tokens to hold and manage the state of the
- split format function up to format, alias and meta
- changes fired for constructed attributes
- change the models constructor arguments to an options object
+- store added
#### v0.5 ####
View
50 store.js
@@ -0,0 +1,50 @@
+goog.provide('mvc.Store');
+
+goog.require('mvc.Model');
+goog.require('goog.events');
+goog.require('goog.object');
+
+/**
+ * @constructor
+ * @param {function(new:mvc.Model,...*)} opt_defaultModel
+ */
+mvc.Store = function(opt_defaultModel) {
+ this.cache_ = {};
+ this.default_ = opt_defaultModel || mvc.Model;
+};
+
+/**
+ * takes two arguments. The model's id (or leave out to create a new model)
+ * and the type of model to create if none found in cache
+ *
+ * @param {string=} input the model's id
+ * @param {function(new:mvc.Model,...*)=} the type of model to create
+ */
+mvc.Store.prototype.get = function(input, opt_model) {
+ if(this.cache_[input])
+ return this.cache_[input];
+ var modelConstructor = opt_model || this.default_;
+ var model = new mod();
+ if(input)
+ ret.set('id', input);
+ this.cache_[input || model.cid_] = model;
+ var list = goog.events.listen(model, goog.events.EventType.CHANGE,
+ function() {
+ var id = model.get("id");
+ if(id){
+ this.cache_[id] = model;
+ delete this.cache_[model.cid_];
+ goog.events.unlistenByKey(list);
+ }
+ }, false, this);
+};
+
+/**
+ * this is used to setup the store from cache
+ *
+ * @param {Object} obj
+ */
+mvc.Store.prototype.set = function(obj) {
+ goog.object.extend(this.cache_, obj);
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.