2

I've only started using Spine.js and it's really enjoyable to user. Does anyone know if Spine handles related objects from a server response? example below. If it does - great - if not? then any suggestions of how to do this would be great! Thanks

Models

class User extends Spine.Model
  @configure 'User', 'Name'
  @hasMany 'friends', 'models/Friend'
  @extend Spine.Model.Ajax

class Friend extends Spine.Model
  @configure 'Friend', 'User_id'
  @belongsTo 'user', 'model/User'
  @extend @Local

Server Response to User update:
{"user":{"name":"John Brilliant", "friends":[{"user_id":1},{"user_id":2}] }}

So, When Spine receives this response, should it update the user and the users friends because of the related model?

just__matt
  • 484
  • 6
  • 15

1 Answers1

0

Check out relation.js that comes with Spine. http://spinejs.com/docs/relations

However there are some issues with nested resources I noticed (creating and updating friends using /users/1/friends etc). Check http://groups.google.com/group/spinejs/browse_thread/thread/6a5327cdb8afdc69?tvc=2 for more info.

If the user will always be the currently logged in user I would suggest not creating a relationship between the 2. Only your server needs to know about multiple users. Your client app should assume that all actions/models/records are scoped to the currently logged in user. On the server side just check to see if your user is logged in and if so only return the friends for the current user. (otherwise redirect to a loginpage)

Let's say friend 3 belongs to user 2. If user 1 logs in and he requests the friends the server would only return friends 1 and 2. If user 2 is logged in it would return only friend 3.

Don't forget to use the current user on the show, create, update and delete actions as well :p

If you need the user's info just make an action on the server like '/users/current' and return the info you need. You could store this in the User model which you could then use to update the user's name etc.

SpoBo
  • 2,100
  • 2
  • 20
  • 28
  • So just to clarify SpoBo, if my User model receives one response object containing both User and Friend objects, Spine will update both User and Friend models with relevant data? and then also, when the User model is being updated via Spine Ajax, will it send both User and Friends objects to the server in one request also? – just__matt Feb 17 '12 at 00:45
  • if my User model receives one response object containing both User and Friend objects, Spine will update both User and Friend models with relevant data -- Spine does this by default yes. when the User model is being updated via Spine Ajax, will it send both User and Friends objects to the server in one request also? -- I don't think so. At least I couldn't get it working. Instead y server uses nested routes to update friends one by one and I made some modifications to Spine to allow for that workflow. But tell me. Why are you using a user object? – SpoBo Feb 17 '12 at 13:42
  • And that works if the server repsonse is in the format above (see original question)? The question models are theoretical, although I do actually work with a user object, most things are decoupled by some are not and I didn't build the API. Thanks for your help btw – just__matt Feb 17 '12 at 14:32
  • I misunderstood your original question. My bad :p After an Ajax call is successful Spine calls a callback function which updates the attributes (for example the local ID with the newly created ID on the server). You can check out the source in ajax.js or ajax.coffee. The method is called recordResponse. For as far as I know though it does not update the relationship models if the update response contained those. You could bind on the ajaxSuccess event and see what that gives you. – SpoBo Feb 17 '12 at 15:46