Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider adding an initialize to the model constructor #23

Closed
weepy opened this issue Nov 13, 2012 · 7 comments
Closed

consider adding an initialize to the model constructor #23

weepy opened this issue Nov 13, 2012 · 7 comments

Comments

@weepy
Copy link

weepy commented Nov 13, 2012

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
Copy link
Member

tj commented Nov 13, 2012

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

@weepy
Copy link
Author

weepy commented Nov 13, 2012

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
Copy link
Member

tj commented Nov 13, 2012

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

@avetisk
Copy link

avetisk commented Nov 13, 2012

initialize?

@RGBboy
Copy link

RGBboy commented Dec 13, 2012

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
Copy link
Member

tj commented Dec 13, 2012

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

@karlbohlmark
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants