简体   繁体   English

将模型传递给对象文字中的Backbone View

[英]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'). 我想传递“事物”参数(“ 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. 如果使它更容易,我将使用骨干,下划线和jquery(如果它们提供了更好的解决方案)。

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! 谢谢你们每一个人的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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