Before you start ctrl+c & ctrl+v'ing some source code, make sure you have tried the hello world sample and read the Rest API sample on the sproutcore wiki. Also, make sure you have played with FireBug to call some of the sproutcore commands shown in the sproutcore tutorials.
Ever since WWDC 2008 announced SproutCore, I got curius with how this javascript mvc framework communicates with a server backend. Since I have very limited knowledge of RoR rest, I decided to stick to what I know - WCF.
For my sample, I am using the contacts example that is posted on the sproutcore googlecode website.
JSON & DTO's
Let's start with how the Contacts JSON object should look like:
Notice how SproutCore collections has a defined format: {"records":[], "ids":[]}, also take notice on how the properties are defined: "first_name", "last_name" - Looks like sproutcore does some nyahh and puts underscores.
Now let's look @ the ContactDTO & SproutCoreContactDTO DataContracts in C# and let's make this work for sproutcore:
This is how we build the SproutCoreContactDTO object:
WCF Service & Web.Config
Now we've built out dto and have an idea how we build our JSON object, let's make WCF do some crazyness and send and take in DTO's as JSON from something that is not a .Net proxy!
Here's the service:
Notice how my methods are List, Create, Update, Show? That's taking advantage of sproutcore's built-in Record functionality.
Note that complex types like ContactDTO being consumed as a parameter must be a WebInvoke and an HTTP-POST method - you will have pain if u dont!
The key words to return your objects as JSON in WCF is "ResponseFormat = WebMessageFormat.Json"
Web.config (system.servicemodel configuration)
Things to remember about your wcf service:
1) must be webHttpBinding to talk to any non-microsoft clients
2) must have a webHttp endpoint behavior
NUnit Tests
We could create a proxy to verify if our services work, but since we're going to be using sproutcore as our UI, we're going to do it the hard way. We're going to bust some serious System.Net skillz... soo peep this awesomeness:
Now that's some awesome shizzle! all works, and it's all good in the hood!
Apache, Mongrel, IIS - Fo Shizzle my Nizzle!
In order for your sc-server (mongrel) to have the proper resourceUrl for your WCF web service, we need to use a middleman as our reverse proxy - apache. Soo, we need to do the following:
1) download apache web server
2) make sure your port is something other than port 80 (I was running IIS and Apache on the same computer)
3) uncomment the following on httpd.conf:
- proxy_module
- proxy_ajp_module
- proxy_balancer_module
- proxy_connect_module
- proxy_http_module
4) add the following in the very bottom of httpd.conf:
#SproutCore Stuff
ProxyPass /contacts http://localhost:4020/contacts
ProxyPassReverse /contacts http://localhost:4020/contacts
ProxyPass /static http://localhost:4020/static
ProxyPassReverse /static http://localhost:4020/static
#IIS Stuff - Where my ContactService.svc is at
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
With ReverProxy set-up, you can still go to localhost:4020, but your reference to your wcf service won't work. Depending on how you configured you apache server, you now have to access your sproutcore site via apache.
Finally! some sproutcore action!!
I'm assuming you have already exported the contacts sample from google code, and read "Using the built-in REST API".
We need to modify a couple of things so we can talk to our wcf service.
Model\contact.js
contacts\main.js
The Finished Product
With all your sc-gen, iisreset, and all that good stuff, run your site to see the final product.

And ofcourse, what's this long post without the sourcecode? download it from here or if you have svn, type "svn co http://ezed.googlecode.com/svn/spikes/SproutCore&WCF"
