Towerjs frontend and rails backend?

696 Views Asked by At

I started in webprogramming with rails since 2 month, and recently towerjs.

I have an existing rails 3.1 backend, and i would use towerjs as the frontend. that means, rails have an existing logic and db.

Could i use towerjs on the browser side, to get and set data with rails into the db?
my present idea is to start an rails and towerjs server, and both communicate together with json. but i don't now how i could implement these!

2

There are 2 best solutions below

0
On

Tower is tightly coupled for running code on the client and server.

You would be better off looking at something like backbone.js because it is indifferent to your server side setup.

I am currently porting a rails app to a single page app. After much hacking, I settled on backbone for the client, node.js/express for the server, and refactored all the rails models to a ruby/grape API. The node server proxies requests to the grape api using the awesome request npm module. Very happy so far.

backbone: http://documentcloud.github.com/backbone/

grape: https://github.com/intridea/grape

2
On

You can use Tower.js purely on the client side.

The documentation [currently] focuses a lot on generating an app like you would Rails, giving you boilerplate for javascript testing, configuring your server and databases, etc. However, you can just require underscore and tower in the browser, build whatever models you need on the client, and connect them through the ajax store to your Rails backend.

<script src="/javascripts/vendor/javascripts/underscore.js"></script>
<script src="/javascripts/vendor/javascripts/tower.js"></script>

Documentation is still needed here (as well as an example), but it works as a client-only MVC. Something like this:

class App extends Tower.Application
  @bootstrap: (data) ->
    App.Post.load(data.posts) if data.posts

class App.Post extends Tower.Model
  @field "title"
  @field "body"

And the HTML file to load the JSON string into the model initially

<body>
  <script>
    App.Post.bootstrap(<%= @posts.to_json %>);
  </script>
</body>