Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js model handling when urlroot returns null

I am relatively a newbie to backbone.js and am working on a sample project with backbone frontend and backend based on node.js. I have two entities: User and Bank in the backend and backbone.js models corresponding to these two models with same names. User and and Bank have one-to-one relation.

The urlroot for the bank model is /bank and that corresponding too user is /user. Now the problem. A user may or may not have a bank entity associated with him. So when a user having a bank account is logged in the backbone model loads the bank corresponding to the logged in user.

How will I handle the backbone.js model for bank if the active user does not have a bank associated with him ? Currently I am returning null if a user does not have a bank associated with him. I feel that this is not the right approach. Any help on this regard is appreciated.

The code corresponding to /bank route is

app.get("/bank/:id?", function(req, res) {
  if (!req.user) {
    return JsonRenderer.error(null, res, 401, false);
  }
  return Bank.findById(req.user.id, function(err, bank) {
    if (err) {
      console.error(err);
    }
    if (bank) {
      return res.json(JsonRenderer.user(bank));
    }
    if (!bank) {
        return JsonRenderer.error('No User', res, 200, false);
    }
  });
});
like image 593
plbit Avatar asked Apr 25 '26 09:04

plbit


1 Answers

I guess if it does what you want then it's okay. But I think it's kind of an interesting question about semantics and how the semantics shape the logic so I'll throw in my answer and see if someone has a better way of thinking about this. I want to learn something too.

From what I read in your description I think about this problem a little differently. Namely, your usage of null and how it's handled as a response.

In my mind, a person that hasn't entered a bank account in the database would be undefined. Nothing has been defined. This would be someone who like, just created an account but hasn't gotten to that part yet. A person who has explicitly said that they don't have a bank account might be null, an object that sort of represents "nothing". Some people would not want to mix these two in their DB column. There is a lot of hubbub about the difference and apparently this is a very JS thing that drives non-JS programmers rabid. ;-)

So given that, I think there is some kind of missing step here which is to know before you make a request for a resource (the bank) whether you should even bother to request it or not. For example, if User and Bank are one to one, you might consider adding some reference to the Bank via id or null or undefined in the User object. This way, when you get the User you could see whether you need to lazily load the bank information if there is one associated with it. If null (or undefined), you don't need to. If it has a BankID like, user.get('bankID'); then you can create an empty model with model.id = bankID and then .fetch(). Thus you only create a Bank model if you need one. Now your success error callback doesn't have to deal with two issues - figuring out if Bank exists, and figuring out about the User. Which is kind of RESTful, since a URL in pure form is about a resource we want. We might want to know if we want the resource before asking for it. Sounds like you're creating a Bank model, then fetching for it but not knowing whether there is one. So you end up with a default bank. Which might not be bad. It might just be an empty representation of a bank. In which case, maybe another way of thinking about it is that everyone DOES have a Bank, just that some Bank information has been filled out and others have not. Like, I might not fill out my profile but I probably still have a profile object created for me when I register. :-)

There is another way to think about this too. Which is, maybe Banks should have some sort of pointer to the User. Parse.com which uses an old fork of Backbone uses this method. Basically I might have something like Bank.user pointing to the User model/object. So on the client side, my user is loaded, then I make a query for a Bank object that includes a pointer to the User I want. This query in Parse just returns undefined if it can't find anything but will return the bank if it matches the query based on the user.

Anyway, I thought it was an interesting question.

like image 185
jmk2142 Avatar answered Apr 27 '26 21:04

jmk2142



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!