You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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;
Added a method to CappuccinoResource class to be overridden in all subclasses that have associations
(void)convertAssociations
{
//Overide in Model classes
}
added a method call to + (id)new:(JSObject)attributes right before the return statement:
[resource convertAssociations];
In parent object, for example Post.j added the declaration
CPArray comments @accessors;
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.
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.
class Post < ActiveRecord::Base
def to_json(args)
super( :methods => [:comments])
end
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;
Added a method to CappuccinoResource class to be overridden in all subclasses that have associations
{
//Overide in Model classes
}
added a method call to + (id)new:(JSObject)attributes right before the return statement:
[resource convertAssociations];
In parent object, for example Post.j added the declaration
CPArray comments @accessors;
and overrode -(void)convertAssociations method
{
[self setComments:[Comment collectionDidLoad:comments]];
}
The child class, in this case Comment.j, I set up just like a regular CappuccinoResource subclass.
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.
The text was updated successfully, but these errors were encountered: