Backbone.js model being processed when urlroot returns null

I am relatively new to backbone.js and I am working on a sample project with a basic interface and node.js based backend . I have two objects, User

and Bank

in the backend and backbone.js models, corresponding to these two models with the same names. User

and Bank

have a one-to-one relationship.

The URL for the bank model /bank

and the corresponding user is /user

. Now the problem. The user may or may not have an associated bank associated with him. Therefore, when a user having a bank account registers in the base model, the bank corresponding to the registered user is loaded.

How would I handle the backbone.js model for a bank if the active user doesn't have a bank associated with it? I am currently returning null if the user does not have a bank associated with it. I feel this is the wrong approach. Any help in this regard is appreciated.

The code corresponding to the route /bank

,

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);
    }
  });
});

      

+3


source to share


1 answer


I think if it does what you want, then it's okay. But I think this is an interesting question about semantics and how semantics shapes logic, so I'll write my answer and see if anyone has a better way to think about it. I also want to know something.

From what I read in your description, I think about this issue a little differently. Namely your use of null and how it is treated as a response.

In my opinion, a person who has not entered a bank account in the database will undefined

. Nothing has been defined. It would be someone who would like to have just created an account but haven't gotten into this part yet. The person who said bluntly that they did not have a bank account might be null

an object that represents "nothing." Some people would not like to mix the two in their DB column. There is a lot of hype about the difference and it appears to be a very JS thing that makes non-JS programmers go berserk .; -)



So I think there is some missing step here that needs to be known before you make a request for a resource (bank), whether you want to even bother requesting it or not. For example, if User

u Bank

are one-to-one, you might consider adding some bank reference via id or null or undefined in the object User

. This way, when you get a User, you can see if you need lazy loading of banking information, if there is one associated with it. If null (or undefined) you don't need to. If it has a BankID, user.get('bankID');

you can create an empty model with model.id = bankID

and then .fetch()

. This way you only create the bank model if you need it. Now your callback issuccess

error

should not deal with two problems - to find out whether the Bank exists and to find out what a User is. What a kind of RESTful, since a pure url is the resource we want. We might want to know if we want to get a resource before asking for it. It looks like you are creating a Bank model and then getting paid for it, but you don't know if there is one. This way you get the default bank. Which might not be bad. It might just be an empty bank view. In this case, maybe another way to think about the fact that everyone has a bank, just that some bank details have been filled in and others have not. For example, I can't complete my profile, but I probably still have a profile object generated for me when I sign up. :-)

There is another way to think about this. That maybe the Banks should have some sort of pointer to the user. This method uses Parse.com which uses the old Backbone fork. Basically, I could have something like Bank.user

pointing to a model / object User

. So on the client side, my user is loaded, then I make a request for a Bank object that contains a pointer to the user I want. This query in Parse just returns undefined if it can't find anything, but returns a bank if it matches a user-based query.

Anyway, I thought it was an interesting question.

+1


source







All Articles