chaplinjs / chaplin

HTML5 application architecture using Backbone.js
http://chaplinjs.org
Other
2.85k stars 232 forks source link

I need help fetching and loading collection from restful api. #8

Closed crussi closed 12 years ago

crussi commented 12 years ago

I'm trying to use Chaplin as a sample scaffold to model my application on. In your Facebook sample you use a service provider to get "likes" and "posts". But, when if I wanted to load an object from a restful api instead? In my current app, that I'm migrating over to using Chaplin's architecture, I have a backbone collection called Notes. By simply calling notes.fetch the collection automatically fetches a json list from my restful api. For some reason, I can't seem to figure out the steps to trigger the fetching of a collection using Chaplin's architecture. In the case of "likes" in the initialize method "@getLikes()" ... which in turn calls the service provider with a callback to @processLikes(). This is all good. But, when how would I go about utilizing the built in capability of a collection to fetch its list from a restful api? I'm writing my application using vs.net 2010 and my backend is the new MVC 4 webapi (that supports restful services).

I'm really loving the architecture of Chaplin. It is quite elegant.

Best regards, Chris

molily commented 12 years ago

Chaplin doesn’t provide an own solution how to sync your models and collections, or imposes any restrictions. That means the standard Backbone way using fetch, save, destroy, sync and so on should work fine, and this is still the preferable way. So you should be able to reuse the existing code as it is.

The likes example is just a special case. It could be forced into the standard Backbone fetch/sync model but that would be less elegant since Facebook doesn’t provide a RESTful API. We’re going to add a more common example using Backbone’s standard sync methods soon.

crussi commented 12 years ago

Hi Mathias, Thanks again for all your responses. I'm truly seeing the elegance of Chaplin and I have been spending many hours studying every part of it. For my notes collection; do you see any problem with fetching the data in my notes_controller? Instead of having the notes initializer call getNotes() and having getNotes() call a service to get data and processNotes() reset the collection; can I have something like below? I'm just not sure where to kick off the collection's fetch. Backbone doesn't have a controller. I want to maintain the same flow as Chaplin. I also want the object (model, collection and view) disposal.

define ['controllers/controller', 'models/notes', 'views/notes_view'], (Controller, Notes, NotesView) ->

'use strict'

class NotesController extends Controller

historyURL: 'notes'

index: (params) ->
  #console.debug 'NotesController#index'
  notes = new Notes()
  notes.fetch()
  @collection = notes
  @view = new NotesView collection: @collection

If this is not how you would go about it; can you please provide some more clarity as to how to kick off a collection getting its own data from a restful api?

Again, thank you for such a beautiful sample application and for the awesome documentation. My hope is that Chaplin will gain traction in the open source community. It is truly an elegant solution.

Best regards, Chris

p.s. I got this approach working in part, but, I am very interested to hear your opinion on the approach I'm taking.

molily commented 12 years ago

Your code should work fine and this is also a straight-forward way. Don’t know what NotesView is (probably a CollectionView), but as long as the fetching is asynchronous, listening to model changes in the view should work.

crussi commented 12 years ago

Thanks Mathias! I now have the restful service returning data. I am moving on to populate the collection view (NotesView).

Best regards, Chris

Sent from my iPad

On Mar 9, 2012, at 1:57 AM, Mathias Schäferreply@reply.github.com wrote:

Your code should work fine and this is also a straight-forward way. Don’t know what NotesView is (probably a CollectionView), but as long as the fetching is asynchronous, listening to model changes in the view should work.


Reply to this email directly or view it on GitHub: https://github.com/moviepilot/chaplin/issues/8#issuecomment-4413629