backbone-orm icon indicating copy to clipboard operation
backbone-orm copied to clipboard

Usage examples

Open tauren opened this issue 15 years ago • 3 comments

It would be really great if you could provide some examples of how this library is used. I'm not asking for full documentation or anything (although that would be sweet!). But some sample code that illustrates the basic features and could help get developers up to speed quickly would help.

Thanks!

tauren avatar Jan 12 '11 19:01 tauren

This project is far from complete, but it was an idea of parsing a single json tree and building complete relations from it.

So if you use rails you would load for exemple in your html output: Projects = new ProjectCollection(<%=Projects.all(:include=>[:committers, :note]).to_json%>); And you define your relation like this in your Project model initializer: //args are the base model, the type of related model, the name of the local attribute and finally the name of the foreign attribute (if you need it) _.extend(this, new Porkepic.EmbeddedInMany(this, Commiter, "committers", "project")) // args: base model, related model type, local attribute _.extend this, new Porkepic.HasOne(this, Note, "note"))

Your can "have" models (so the json looks like {model1_att1: "tata", model1_att2 : {model2_att1: ""}} ) or you can be embedded in another model (meaning you have only a referring id to that model)

I do not have more time right now to move this project forward, but if there is some need it could be done.

I felt it was too heavy for light applications. On big application with heavy data it could be interesting to use this type of api, but it would have to load lazily to avoid long page load.

juggy avatar Jan 12 '11 19:01 juggy

And how do you access the relation? with the get method from Backbone?

elcuervo avatar Feb 22 '11 18:02 elcuervo

Yes you do. Names are based on the relation names you give. In the above example: committers on the project model and project on the committer model.

juggy avatar Feb 23 '11 18:02 juggy