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

Nested Model Hack #7

Open
dbrajkovic opened this issue Sep 30, 2010 · 1 comment
Open

Nested Model Hack #7

dbrajkovic opened this issue Sep 30, 2010 · 1 comment

Comments

@dbrajkovic
Copy link

So here's what I did to get nested models to work. I needed to to this to reduce the amount of http requests and database requests.

  1. In rails, included associations by overriding the to_json method in the model class. This is the only way I know to have rails include the root class for the associations.

class Post < ActiveRecord::Base
def to_json(args)
super( :methods => [:comments])
end

  1. Added a case to the switch function in setAttributes: method in CappuccinoResource class in CRBase.j
    case "object":
    [self setValue:[CPString JSONFromObject:value] forKey:attributeName];
    break;

  2. Added a method to CappuccinoResource class to be overridden in all subclasses that have associations

  • (void)convertAssociations
    {
    //Overide in Model classes
    }
  1. added a method call to + (id)new:(JSObject)attributes right before the return statement:
    [resource convertAssociations];

  2. In parent object, for example Post.j added the declaration
    CPArray comments @accessors;

  3. and overrode -(void)convertAssociations method

  • (void)convertAssociations
    {
    [self setComments:[Comment collectionDidLoad:comments]];
    }
  1. The child class, in this case Comment.j, I set up just like a regular CappuccinoResource subclass.

  2. Now in my controllers I can access the associated models like any other attribute
    [post comments]

I've only tested reading from Rails, haven't saved to Rails yet.

@n9yty
Copy link

n9yty commented May 14, 2011

I have yet to start work on using this, I have a bit more groundwork to put in place first, but I wonder what the future is for this module, it would be great to see progress made on the TODO list. This is a GREAT start.

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

2 participants