簡體   English   中英

從另一個視圖調用視圖並將其傳遞給Backbone模型的最佳方法

[英]Best way to call view from another view and pass it a model in Backbone

如何在另一個視圖中調用視圖並將其傳遞給模型?在我的代碼下面,我將在事件中顯示的#button_cont click處調用另一個視圖。我可以簡單地調用新視圖並傳遞給模型或存在更好的方法嗎?

在呼叫者視圖下方:

define(["jquery", "underscore","Handlebars","models/attore",
"text!templates/backend0.html"],
function ($, _, Handlebars,Attore,template) {

var BackendView0 = Backbone.View.extend({



    template: Handlebars.compile(template),


    events: {

        'click #button_cont': 'twitter_list',

    },



    initialize: function () {


    },

    render: function (eventName) {


       $(this.el).html(this.template());

      return this;


    },



     twitter_list: function () {
        var nome=$('#nome').val();
        var cognome=$('#cognome').val();

        var attore= new Attore({nome:nome,cognome:cognome});

   ---  Here I want call another view and pass it model attore---------
    },


  });

return BackendView0;

如果總是只有2個視圖,則可以通過存儲對另一個視圖的引用來告訴彼此。 然后,傳遞模型的方法可以執行以下操作:

this.otherView.model = this.model;

該引用可以存儲在視圖初始化中,也可以通過setter方法存儲:

setOtherView: function(view){
    this.otherView = view;
}

您無需說兩個視圖是相互獨立的還是層次結構的一部分。 如果視圖確實是分開的,那么它們根本不應該相互“調用”。 每個視圖都應獨立於頁面的任何部分來管理其自己的頁面部分。

如果視圖是層次結構的一部分(例如,集合視圖和模型視圖),則父視圖應負責創建子視圖,在這種情況下,它可以像其他任何視圖一樣將模型作為輸入傳遞給構造函數。

我會這樣做:

var myNewView = new AnotherView({ model: attore });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM