0
votes

I am using Backbone and Marionette.

I have a defined User model:

Users.User = Backbone.Model.extend({
    defaults: {
        short_name: '',
        full_name: ''
    }
});

I want to easily accessed currently logged in user data in my backbone code.

I am not sure whether to :

  1. have a separate Model that inherits from the User Model (Maybe a Users.AuthUser??)

OR

  1. add a separate function inside Users.User model instead that returns me an instance of the User model but containing the logged in user data.

I am also not sure how to retrieve the currently logged in user data.

Do i expose the data through an endpoint url like http://example.com/me as JSON?

Or somehow the Backbone can access the session data?

2

2 Answers

1
votes

It's a general rule of thumb that if the subclass has properties/methods that are of no relevance to the parent then it should be subclassed. In my opinion, this is an example of that.

Backbone (simply an MVC JS implementation) is not able to access your session data. So this will need to be available via a REST request.

0
votes

Data is not shared between models. Unless you need specific function within the user model for authenticated users, it's sufficient to just populate the authenticate use with more data.

The way I do this is in the fetch() method, making sure my model communicated with an endpoint that returns a JSON of the currently logged in user.

In order to define a different urlRoot for authenticated vs. other users, you'll need to have two separate models.

P.S., actually I use socket.io as the sync protocol instead of REST API, so I just sync using different event names for current user vs. contacts.