Backbone js model dependency injection
Is it acceptable to "dependency-inject" more than one models into a View on initialized()
in Backbone?
For example:
var myView = new开发者_Python百科 MyView({
model: {
category: categoryModel,
name: nameModel,
tag: tagModel
}
})
Absolutely, it is acceptable!
There are common practices for working with models and views - most notably, people usually pass a Backbone model in to a view. However, there's no rules for what a view's model
should or should not be.
The real key is that your team (if you have one) understands what you're doing and why. If you're going to use this pattern in your app, then the team needs to know what signs to look for and what the common patterns are for when and why you would do this.
(Along those lines, but not really directly a part of my answers... I would ask: why do you want to do this? Do you really need three separate models to do what your view needs? Or are you perhaps missing an abstraction in the form of a single view model that should encapsulate all of the data you need.)
精彩评论