benpickles / js-model

Work with models in your JavaScript
http://benpickles.github.io/js-model/
MIT License
403 stars 42 forks source link

nested structures possible? #2

Closed fwoeck closed 14 years ago

fwoeck commented 14 years ago

Hi Ben,

thank your for the plugin! Do you have a recommendation concerning nested models with arrays? It seems, that var post = new Post({ foo: ["bar","baz"] }) works well until I save it via REST - rails moans about the array instead of a hash. Is it a bad idea overall, or do you think it would be possible to tweak rails' params-parsing or write an other persistence strategy?

-- Frank

fwoeck commented 14 years ago

ahh sorry, I was sloppy with the routes - saving works via REST for e.g. var post = new Post({foo: ['bar','baz'], one: {two: ['three','four']}}); -- Frank

benpickles commented 14 years ago

Coincidentally I hit a similar issue with an array of objects and the solution was to JSON.stringify the data and upload it with a content type of application/json. It worked great with Rails 3 and required no changes but I haven't tried it with Rails 2 and it could possibly require some changes to server-side code.

I expect js-model v0.9 will do things this way. It makes sense to work with JSON in both directions, what do you think?

fwoeck commented 14 years ago

that sounds good! Two things I noticed:

{"block"=>{"discount"=>"1", "sum_enabled"=>"true", "header"=>"Some title", "tasks"=>{"0"=>{"cost"=>"240", "resource"=>"sysadmin", "time"=>"1", "description"=>"stuff Ä"}, "1"=>{"cost"=>"275", "resource"=>"developer", "time"=>"3.5", "description"=>"stuff Ü"}, "2"=>{"cost"=>"260", "resource"=>"uxdesigner", "time"=>"2.25", "description"=>"stuff ß"}}}}

witch is not critical but would be nice to avoid. The other thing: there is no way to get models from the server from within the persistance layer right? This would be very convenient to have ;)

-- Frank

benpickles commented 14 years ago

Sending JSON (as opposed to form params) fixes the indexed hashes problem.

If you want to load via the persistence layer I would recommend trying @ahe's fork: http://github.com/ahe/js-model. I imagine there will be a few edge cases that will need ironing out but it's pretty much how I see it working. Let me know how you get on.

fwoeck commented 14 years ago

ahh, thank for for the hint - i'll try! -- Frank

benpickles commented 13 years ago

just to say that the latest js-model (v0.9) does indeed send data to the server as JSON instead of application/x-www-form-urlencoded. it also has Model.load - http://benpickles.github.com/js-model/#loading-data

fwoeck commented 13 years ago

thanks for the update :)

PlasticLizard commented 13 years ago

Speaking of nested data, does js-model support, or have a way of simulating, nested models of the kind that come from document databases? I saw in the read-me a way to simulate a relational association, I just wonder if you had a similar trick up your sleeve for an embedded association, so you could do something like MyModel.find("id").child_models.find("child_id").attr("a","b") ?