component / model

Minimalistic extensible data models
122 stars 40 forks source link

consider adding an initialize to the model constructor #23

Closed weepy closed 10 years ago

weepy commented 11 years ago

ie :

  function model(attrs) {
    if (!(this instanceof model)) return new model(attrs);
    this._callbacks = {};
    this.attrs = {};
    this.dirty = {};
    if (attrs) this.set(attrs);
    this.initialize && this.initialize()
  }
tj commented 11 years ago

we could utilize the top-level constructor events instead of adding a bunch of adhoc things like before/after/initialize

weepy commented 11 years ago

ah ok so something like :

User = model('User')
User.on('construct', function(user) {
  // do things with user
})

with this somewhere:

  this.model.emit('construct', this)
tj commented 11 years ago

yeah, not sure what the event name should be but something like that

avetisk commented 11 years ago

initialize?

RGBboy commented 11 years ago

If you went down the event road, how would you handle changes to the model that may be async? Something like:

User = model('User')
User.on('init', function(user, next) {
  // do things with user
  next()
})
tj commented 11 years ago

@RGBboy you can't magically block a constructor anyway, so that isn't possible

karlbohlmark commented 11 years ago

My use case for this would be when you want nested models. (Using the event for performing smth similar to the JSON.parse reviver parameter)