简体   繁体   中英

Spine JS Model: How does Spine handle a server response containing records for a related model?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM