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.

有帮助吗?

解决方案

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

其他提示

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top