2

I need support for collections in spine.js. I know that spine.js doesn't support this at the moment - not sure if it ever will.

Has anybody added this feature or know the best way to go about implementing it?

noah
  • 21,289
  • 17
  • 64
  • 88
Andrew Zielinski
  • 495
  • 1
  • 4
  • 4

1 Answers1

5

This feature is built in.

Collections are just class methods on your model. The recommended way then is to simply add those few methods to your model.

If you need a different class for everything related to a collection (because you are used to ir ot something) you can simply create a new one that inherits from the original model and add the classmethods there.

sample: (check the console output) http://jsfiddle.net/SpoBo/vBtKC/

I could have just as easily moved the published class method to the Post model and all would have worked as well without the need of an extra PostCollection class. Your choice :)

SpoBo
  • 2,100
  • 2
  • 20
  • 28
  • Sorry, I should have been more clear. I need to manage separate collections, for example I have two tables and need to bind each one to their own collection of flights. ( btw sorry for taking so long to respond ) – Andrew Zielinski Mar 15 '12 at 12:41
  • Tables as in HTML tables I suppose. Just make 2 functions on your model. 1 function to return the flights for the first table and another for the second table. For the controllers it's best to use the element pattern (http://spinejs.com/docs/controller_patterns) so you don't have to rerender all elements in both tables when something updates. You could also check out the relations functionality. something has_many flights etc. but that only works nicely if the flights are linked through ids with some other model. – SpoBo Mar 15 '12 at 15:34
  • I don't think 2 functions will work. A 'refresh' event would trigger on all elements, it also doesn't manage duplicate records, i.e. it only keeps the 1 version of the record. I was thinking about using the has_many functionality. Maybe that is the way to go. – Andrew Zielinski Mar 16 '12 at 00:05
  • You could also create a second model if you want 2 completely separate collections. But if you store via AJAX you need to make sure they have 2 separate URL's. But it should be possible to add something like '?type=late' to the URL for the second model which you could then process appropriately on your backend. – SpoBo Mar 16 '12 at 08:18
  • That may suffice for 2 collections even though the solution isn't elegant but problems is that the user may need more than 2, this is a likely scenario. It would not be uncommon to have 6, e.g. a collection of flights for each flight leg. – Andrew Zielinski Mar 16 '12 at 10:34
  • you're best of using relationships then :) a user has many flightlegs and a flightleg can have many bookings and a booking has one flight and one flightleg. – SpoBo Mar 16 '12 at 14:44