2011-10-18 77 views
91

我有一系列按鈕,點擊後會顯示一個位於按鈕下方的彈出菜單。我想將按鈕的位置傳遞給視圖。我怎樣才能做到這一點?如何將參數傳遞到視圖

ItemView = Backbone.View.extend({ 
    tagName: 'li', 
    events: { 
     'click': 'showMenu' 
    }, 
    initialize: function() { 
     _.bindAll(this, 'render'); 
    }, 
    render: function() { 
    return $(this.el).html(this.model.get('name')); 
    }, 
    showMenu: function() { 
     var itemColl = new ItemColl(); 
     new MenuView({collection: itemColl}); // how to pass the position of menu here? 
    } 
}); 

回答

164

你只需要在構造Menu時傳遞額外的參數視圖。無需添加initialize功能。

new MenuView({ 
    collection: itemColl, 
    position: this.getPosition() 
}) 

,然後在MenuView,您可以使用this.options.position

UPDATE:

initialize: function(options) { 
    this.options = options; 
    _.bindAll(this, 'render'); 
}, 

,或者使用一些更精細的方式爲described by @Brave Dave作爲@mu is too short states,因爲1.1.0,Backbone Views no longer automatically attach options passed to the constructor as this.options, but you can do it yourself if you prefer.

因此,在你initialize方法,可以將options通過爲this.options保存。

+53

此不再工作爲1.1.0:「骨幹查看不再自動連接傳遞給構造選項'this.options',但你可以,如果你喜歡自己做。」(HTTP:// backbonejs。組織/#更新日誌)。 –

+7

現在要獲得相同的行爲:http://stackoverflow.com/a/19325531/941764 – jgillich

+1

這工作完全,只需添加參數在您看來初始化方法:初始化:功能(選擇){ 警報(options.position) ; } –

46

添加選項參數initialize

initialize: function(options) { 
    // Deal with default options and then look at options.pos 
    // ... 
}, 

再經過一些選項,當您創建視圖:

var v = new ItemView({ pos: whatever_it_is}); 

欲瞭解更多信息:http://backbonejs.org/#View-constructor

+0

非常有用的鏈接! –

+0

這對大多數情況來說更爲優雅/簡單。 –

+0

@CullenSUN:謝謝。我更喜歡這種方法的明確性,使用'this.options'的神奇「遠方行動」爲我提供了維護和調試噩夢。 –

-1

通從其他位置

new MenuView({ 
    collection: itemColl, 
    position: this.getPosition() 
}) 

添加選項參數鑑於初始化您獲得的是傳遞的變量,

initialize: function(options) { 
    // Deal with default options and then look at options.pos 
    // ... 
}, 

獲得價值 使用 -

var v = new ItemView({ pos: this.options.positions}); 
+5

寫的改進的答案不是集體的。 –

+0

這是改進的答案! –

11

自骨幹網1.1.0起,options參數爲no longer attached自動轉到該視圖(請參閱issue 2458進行討論)。現在,您需要手動附加每個視圖選項:

MenuView = Backbone.View.extend({ 
    initialize: function(options) { 
     _.extend(this, _.pick(options, "position", ...)); 
    } 
}); 

new MenuView({ 
    collection: itemColl, 
    position: this.getPosition(), 
    ... 
}); 

或者您可以使用this mini plugin來自動連接白名單選項,就像這樣:

MenuView = Backbone.View.extend({ 
    options : ["position", ...] // options.position will be copied to this.position 
}); 
-2

使用this.options到鑑於

// Place holder 
<div class="contentName"></div> 

var showNameView = Backbone.View.extend({ 
     el:'.contentName', 
     initialize: function(){ 
      // Get name value by this.options.name 
      this.render(this.options.name); 
     }, 
     render: function(name){ 
      $('.contentName').html(name); 
     } 
    }); 

    $(document).ready(function(){ 
     // Passing name as argument to view 
     var myName1 = new showNameView({name: 'Nishant'}); 
    }); 

工作實施例檢索argumentr:http://jsfiddle.net/Cpn3g/1771/