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

Associations? #3

Open
saimonmoore opened this issue Jul 16, 2010 · 8 comments
Open

Associations? #3

saimonmoore opened this issue Jul 16, 2010 · 8 comments

Comments

@saimonmoore
Copy link

Any plans for rudimentary associations between models? How are you doing this now?

@benpickles
Copy link
Owner

Hi Saimon,

at the moment I declare a method on each side of the association so a Mat has many Cats:

var Cat = Model("cat", {}, {
  mat: function() {
    var mat_id = this.attr("mat_id")

    return Mat.detect(function() {
      return this.id() == mat_id
    })
  }
})

var Mat = Model("mat", {}, {
  cats: function() {
    var id = this.id()

    return Cat.select(function() {
      return this.attr("mat_id") == id
    })
  }
})

I haven't really thought about proper associations - by "proper" I suppose I mean something like ActiveRecord's declarative syntax - but it would certainly be possible to encapsulate the above logic into less code.

What kind of thing were you thinking?

@saimonmoore
Copy link
Author

yeah I was thinking of AR-style. I also do what you're doing right now but I was wondering wether you had intentions of making it more declarative. If you haven't done it by the time I get to it, I'll give it a stab.

@benpickles
Copy link
Owner

Sounds good to me!

@tyok
Copy link

tyok commented Oct 14, 2010

I'm currently working on this feature. It's still in testing phase, but I'm expecting the first iteration to work like this:
var Post = Model("post");
var Comment = Model("comment");

Post.hasMany(Comment);
Comment.belongsTo(Post);

var p = Post.first();
p.comment(); // return []

var c = new Comment();
c.post(); // return null

c.attr("post_id", p.id());
c.post(); // return p
p.comment(); // return c

Let me know what you think. It's kinda half-working right now, I need to test it more thoroughly.

@saimonmoore
Copy link
Author

sounds good...I never managed to get time to finish off what I started but it was along these lines...

@benpickles
Copy link
Owner

cool, looks good. let me know when you have something to look at on github. in the meantime i've reopened the issue so i don't forget.

@tyok
Copy link

tyok commented Oct 18, 2010

I have pushed my branch:

http://github.com/tyok/js-model/tree/relationship

@benpickles
Copy link
Owner

sorry for my belated response, it looks good.

i've recently been thinking about some sort of plugin architecture™ to more easily enable this sort of thing so that you could extend js-model's functionality without having to mess around with the internals - something along the lines of sammy's use.

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

3 participants