Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having more models in a viewmodel when using "factories" #153

Closed
mlarsson opened this issue Mar 22, 2016 · 4 comments
Closed

Having more models in a viewmodel when using "factories" #153

mlarsson opened this issue Mar 22, 2016 · 4 comments

Comments

@mlarsson
Copy link

Hey kmalakoff

I'm facing a problem while using your otherwise wonderful framework. In applesVM below I'm using "factories" to automate the creation of a new viewmodel, a AppleViewModel, when a new apple is added to the applesModel.apples Backbone collection. But the new AppleViewModel also needs access to the singleton instance of my pearModel. But I can't figure out how to inject this model into the new instances of my AppleViewModel. How do I do this in Knockback?

Kind regards Morten Larsson

var AppleModel = Backbone.RelationalModel.extend({});

var PearModel = Backbone.RelationalModel.extend({});


var ApplesModel = Backbone.RelationalModel.extend({
    relations: [{
            type: Backbone.HasMany,
            key: "apples",
            relatedModel: AppleModel
        }
    ]
});

var AppleViewModel = kb.ViewModel.extend({
    constructor: function (appleModel, options) {
        //How do I get access to pearModel here as well???
    }
});

var applesVM = kb.ViewModel.extend({
    constructor: function (applesModel, pearModel) {
        var self = this;
        self.apples = kb.collectionObservable(applesModel.get("apples"), {
            factories: {
                "models": AppleViewModel
            }
        });
    }
});
@kmalakoff
Copy link
Owner

Glad to hear you've enjoying it.

Hmmmm. If there was a single model with both apples and a pear, and you configured the reverse relationships for each, and you could traverse the model relationships from apple to apples then to pear when passed in.

var ApplesModel = Backbone.RelationalModel.extend({
    relations: [{
            type: Backbone.HasMany,
            key: "apples",
            relatedModel: AppleModel
        }, 
        {
            type: Backbone.HasOne,
            key: "pear",
            relatedModel: PearModel
        }
    ]
});

// plus add the reverse relationships between Apple -> Apples, Pear -> Apples.

Would that work?

@mlarsson
Copy link
Author

Thank you for your quick response. Yes that would work but it would create an unwanted relation between the models. Apple(s) and the Pear model I mean. They don't really relate to each other on the model level. What I'm search for is for the viewmodel (AppleViewModel) to have access to the PearModel. Can this be done?

@mlarsson mlarsson reopened this Mar 22, 2016
@kmalakoff
Copy link
Owner

Most of the time the combination of model relationships or traversing the $data stack in the view using Knockout cover most cases.

I'm trying to think if there is a work around to pass the data through and the best I can think of is using a constructor for the view models. See create API examples like this and this. If you need this also for the model (non-collection) case, you can also look at the view model tests for create examples.

Basically, you have the option to use a create function install of passing a reference to the view model constructor in the factory. Then you can create an instance of your view model and pass whatever parameters you want to the constructor.

Would this work for you?

@mlarsson
Copy link
Author

Yes that is exactly what I was looking for! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants