weepy / o_O

Funnyface: HTML binding for teh lulz
http://weepy.github.com/o_O/
MIT License
149 stars 11 forks source link

o_O.store / o_O.factory #36

Closed weepy closed 12 years ago

weepy commented 12 years ago

It might be worth removing the logic in the model that deals with .id and .type , and moving them to a store.

It would be a memory store by default, but easy enough to plugin different stores.

.save would probably assign an id

troygoode commented 12 years ago

+1 as an o_O.memorystore.js plugin, -1 to being built into o_O.js directly

weepy commented 12 years ago

added a sketch for how this might work

weepy commented 12 years ago

here's a sketch - what do you think ?

o_O.memoryStore = function() {
  this._id = 0
  this.objects = {}
  this.types = {}
}

var proto = o_O.memoryStore.prototype

proto.uniqueId = function() {
  return ++this._id
}

proto.find = function(id) {
  return this.objects[i]
}

proto.add = function(object) {
  var id = object.id
  if(id == null || !this.find(id) ) {
    object.id = id || this.uniqueId()
    this.objects[id] = object
  }
}

proto.remove = function(o) {
  delete this.objects[o.id]
}

// merge in some methods to o_O.model
o_O.model.protoype.store = new o_O.memoryStore()
o_O.model.protoype.save = function() {
  this.store.add(this)
}

// o_O.model already acts as a factory
// doesn't makes sense to move this to o_O.store
o_O.model.types = {...}
o_O.model.create = function(){ ... } // perhaps better to call something like `instantiate` - although a bit long

// how do deal with loading id's and so should update the _id so that the unique id
// whenever adding an object - check to see if object.id >= this._id ?
weepy commented 12 years ago

Hmm thinking about it - I see your point I think. What is the use of a memory store ? -- as we already have the objects ^_^

ok to reserve id, and type and keep them as normal properties rather than o_O.properties ?

troygoode commented 12 years ago

why would we not want type and id to be o_O.properties?

weepy commented 12 years ago

records don't change their id's or types ?

troygoode commented 12 years ago

That is true, but why special-case it?

weepy commented 12 years ago

a fair point . perhaps it's silly

weepy commented 12 years ago

closing