Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Unique attributes #60

Open
TheBox193 opened this issue Jul 1, 2014 · 3 comments
Open

Unique attributes #60

TheBox193 opened this issue Jul 1, 2014 · 3 comments

Comments

@TheBox193
Copy link
Contributor

I've been really enjoying the power and elegance of supermodel, thanks so much!

I have a case where I can retrieve models from my backend via an additional unique attribute other than id - in my case username. In the cases where I .create({id: 10}) we naturally check for an existing model. However when .create({username: 'TheBox193'}) there is no id to match and thus a new model is created. I could programmatically change the idAttribute temporarily, however that seems it might be less than ideal.

Since models (for any applications) could have several unique attributes (id, username, email, etc.) would it make sense to match models based off of additional attributes to reduce duplicates? My thought would be to add a uniqueAttributes on the model that would define additional attributes a model could be matched by for the application.

I could write a draft of this up if it would make sense.

@braddunbar
Copy link
Contributor

Hi @TheBox193! When I've come up against this problem in the past I've just fetched the model via $.ajax or similar and then instantiated the model with the id returned. Any reason that doesn't work in your case?

@TheBox193
Copy link
Contributor Author

Mainly when a user navigates to a route based on username in my app I encounter the issue of duplicates. Links / routes use the 'human' friendly username instead of userid, when navigating to another route/view by username the username because the only unique identifier available. (At least in the case of a Backbone.Marionette app). It would would be nice to pass along the id with the route change in addition, but sadly I only have the username to work with. example.com/user/TheBox193.

I could do a custom $.ajax (or more likely a custom Backbone.sync), however that will take additional coordination and time. If the model is already in memory and can be found by another unique attribute, ideally we can find and use it (and also avoid a duplicate).

I do have some functional code running for this already, I'm liking it but going to do some further testing to make sure it is good to be shared.

@TheBox193
Copy link
Contributor Author

The PR #64 I put in will support .create({username: 'TheBox193'}) and will match on username provided that in the model username is whitelisted in the uniqueAttribute on the model's definition.

var User = Supermodel.Model.extend({
   uniqueAttributes: ['username']
});

Matching by uniqueAttributes is prioritized as last - after matching by model attrs and id per usual.

Todo:

  • Write up some tests
  • Add to docs
  • Lint

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants