문제

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