1

I don't know any better way to ask this question than to give my situation.

I have a reader application that needs to be made, on the page it will have the Table of Contents on the left side and the actual content of the book on right. The TOC content shows chapters and sections within the chapter. The content on the right will only shows one of those sections at a time, no preloading the whole book. When you click on the section in the TOC, makes an API call to a separate server that holds all the book data and returns the HTML to be displayed in the content section.

I want to know if there is a way to make the application to use both unobtrusive javascript and active resource. Active resource would handle the interaction between the Rails Application and the server that holds the book content. The unobtrusive javascript would be the controller between the table of contents and the content on the right.

I guess what I'm wondering most of all, beyond just its possibility, is if I can integrate this is such a way that I don't need to make unnecessary api calls. Currently, I can't figure out how this would work without it making an api call to rails then making another api call to the book content provider.

Thanks for your help in advance!

@tab

Edit:

The Content model would be used to talk to the external API using ActiveResource. UJS would be used to call that model and display that returned content on the page. So the flow would sort of be like, click link myapp.com/book.js. This would go to the controller book#index that would initialize a variable that gets its data from the content model. The content model would use ActiveResource to make an API call to the external content server and would return html. The html that would be returned would be filtered back out and displayed on the page.

There will certainly be some caching, but the page itself will also need to be dynamic enough to allow editing of the content.

Eric C
  • 2,195
  • 1
  • 17
  • 23
  • So you want something like a Content model that implements the logic of talking to the external API, a ContentsController#show that will ask an instance of the model to get the desired content, and then AJAXify it using UJS? I'm not clear on the part about cutting out the Rails API in the middle - otherwise how are you going to use ActiveResource at all? Maybe you want to cache the content? – dantswain Oct 11 '11 at 19:59
  • See above edit for further explanation. – Eric C Oct 11 '11 at 21:05
  • So if I'm understanding, it's definitely possible. Not really sure if there's still a question? – dantswain Oct 12 '11 at 01:15

1 Answers1

0

I was able to make a test program that used unobtrusive javascript to call a model using an API call.

It works the exact same as any other unobtrusive javascript setup, but instead the controller makes a call to the activeresource model. It acts no differently, I suppose I shouldn't be surprised.

Eric C
  • 2,195
  • 1
  • 17
  • 23
  • If this is what worked, you should give yourself answer credit :) some more details on how you implemented it would be helpful to anyone who finds this by googling the same or a similar question. – dantswain Oct 13 '11 at 17:22