0

如何在运行时将值动态传递给 Marionette.CompositeView?就像在java中我们创建一个如下的方法

package com.test.poc;

public class SampleMethod {
    public int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        SampleMethod method = new SampleMethod();
        int firstValue = 90, secondValue = 90;
        System.out.println("add : " + method.add(firstValue, secondValue));

    }
}

以上是任何人都可以像上面一样理解的简单java代码,如何创建和传递参数Marionette.CompositeView并对其进行处理?

此致

4

1 回答 1

5

在你实例化一个视图的那一刻,你可以传递你想要的任何参数。通常你传递模型和要在复合视图中呈现的集合,但如果需要,你可以传递更多数据。

 var MyCompositeView = Backbone.Mationette.CompositeView.extend({
     initialize : function (options){
        this.dataValue = options.dataValue1;
        this.helperObject = options.helperObject;
        this.useValues();
     },
     useValues: function () {
       console.log(this.dataValue);
     }

 });

 var helperObject = { 
     value3 : "I have a value",
     value4 : "I dont!"
 }; /// a js object literal

 var myModel = new MyModel();
 var myCollection = new MyCollection();
 var myCompositeView = new MyCompositeView({model:myModel,
                                            collection:myCollection,
                                            helperObject:helperObject,
                                            dataValue1:"Hi there"});

请注意,我当时传递了 4 个值来激活视图,并且我只读取其中两个,模型和集合将由木偶处理,但另外两个您可以在初始化函数中读取它们。

希望有帮助。

于 2013-07-15T15:11:37.307 回答