简体   繁体   中英

Passing model to Backbone View in object literal

I'm not sure this is possible, but I would like to pass parameters to functions in an object literal.

Here is my object:

var object_literal = {
    "thing" : new APP.SomeView({ model : model_to_pass, secondPart : static_model})
};

Then I wanted to loop though a collection, grab a reference

someCollection.each(function(things) {
    //will equal "thing"
    var x = things.get("reference");

    //Then do something like this (which obviously won't work)
    layout.someRegion.show(object_literal[x](model_to_pass))
}

As you can see, the 'thing' is a function (in this case a backbone view). I would like to pass 'thing' a parameter ('model_to_pass'). How can this be done?

If it makes it easier, I'm using backbone, underscore and jquery, if they offer better solutions.

I've decided this was a bit over-engineering for the solution, and using conditionals would be the best approach.

someCollection.each(function(things) {
    //will equal "thing"
    var x = things.get("reference");

    if (x === "thing") {
        new APP.SomeView({ model : model_to_pass, secondPart : static_model}
    }
}

Thank you everyone for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM