Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 MyView({
    model: {
        category: categoryModel, 
        name: nameModel, 
        tag: tagModel
    }
})
like image 294
trivektor Avatar asked Oct 13 '11 01:10

trivektor


People also ask

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.

Does anyone use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is backbone JS used for?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


1 Answers

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

like image 196
Derick Bailey Avatar answered Sep 28 '22 13:09

Derick Bailey