Question

Is there a way to create an alias in a Backbone Collection?
So collection.get("currentSite") would return the model with id "stackOverflow". Also set and events etc. would work the same.
The purpose is to reference a state model for the current host, without looking up the current id every time the model is needed.

Was it helpful?

Solution

You could add the alias attribute to your model

Then you can get the model from your collection using where

var mySite = collection.where({alias: "currentSite"})

OTHER TIPS

Not a direct answer to you question, but you might be able to solve your problem using Backbone.Picky https://github.com/derickbailey/backbone.picky

It makes a collection have a "selected" model, so as your state changes, simply set the selected attribute on the correct model. Later, you can fetch the selected model easily (and the "get", "set", etc. will work as usual).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top